SpringBoot实现文件上传功能
新建maven项目,pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.niiam</groupId>
<artifactId>SBFileUpload</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SBFileUpload Maven Webapp</name>
<url>http://maven.apache.org</url> <!-- 引入springboot组件 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入json组件 -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency> <!-- 使得内嵌的Tomcat不可见,用于导出war包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> </dependencies> <!-- 制定Java编译版本,用于消除IDE关于JRE版本的warning -->
<properties>
<java.version>9</java.version>
</properties> <build>
<finalName>SBFileUpload</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories> </project>
Test.html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> </head>
<body>
<form id="formId" action="/SBFileUpload/testUpload" target="frame1" method="POST" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="button" value="提交" onclick="upload()">
</form> <iframe name="frame1" frameborder="0" height="40"></iframe> <script type="text/javascript">
function upload() {
$("#formId").submit();
}
</script> </body>
</html>
注意:
1、第10行,action里要指明项目名,这样在跳转时才能跳转到该项目的链接中
2、为了防止点击提交按键后页面跳转,此处设置了iframe标签,用于点击按键后发送ajax指令。
后台Java代码:
Application.java
package com.niiam; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication
public class Application extends SpringBootServletInitializer{ @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
} public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Controller.java
package com.niiam; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod; import java.io.*; import javax.servlet.http.HttpServletRequest; @RestController
public class Controller {
@RequestMapping(value="/testUpload",method=RequestMethod.POST)
public void testUploadFile(HttpServletRequest req,MultipartHttpServletRequest multiReq) throws IOException{
FileOutputStream fos=new FileOutputStream(new File("E://fileuploadtest//src//file//upload.jpg"));
FileInputStream fs=(FileInputStream) multiReq.getFile("file").getInputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=fs.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fos.close();
fs.close();
}
}
如果上传的文件大于 1M 时,上传会报错文件太大的错误,在 application.properties 中设置上传文件的参数即可
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb
application.properties可以自己新建,放在这里:(该文件可以放在4个地方,详情百度)
三种方法测试:
1、右键点击Test.html,选择run on server
2、项目Export为war包,放在Tomcat上运行
结果展示:
SpringBoot实现文件上传功能的更多相关文章
- springBoot的文件上传功能
知识点: 后台:将上传的图片写入指定服务器路径,保存起来,返回上传后的图片路径(在springBoot中,参考博客:http://blog.csdn.net/change_on/article/det ...
- Springboot如何启用文件上传功能
网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...
- SpringBoot图文教程4—SpringBoot 实现文件上传下载
有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...
- PHPCMS_V9 模型字段添加单文件上传功能
后台有“多文件上传”功能,但是对于有些情况,我们只需要上传一个文件,而使用多文件上传功能上传一个文件,而调用时调用一个文件URL太麻烦了. 使用说明: 1.打开phpcms\modules\conte ...
- 配置php.ini实现PHP文件上传功能
本文介绍了如何配置php.ini实现PHP文件上传功能.其中涉及到php.ini配置文件中的upload_tmp_dir.upload_max_filesize.post_max_size等选项,这些 ...
- MVC5:使用Ajax和HTML5实现文件上传功能
引言 在实际编程中,经常遇到实现文件上传并显示上传进度的功能,基于此目的,本文就为大家介绍不使用flash 或任何上传文件的插件来实现带有进度显示的文件上传功能. 基本功能:实现带有进度条的文件上传功 ...
- Spring 文件上传功能
本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: <dependency> <groupId> ...
- Spring +SpringMVC 实现文件上传功能。。。
要实现Spring +SpringMVC 实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...
- 用c++开发基于tcp协议的文件上传功能
用c++开发基于tcp协议的文件上传功能 2005我正在一家游戏公司做程序员,当时一直在看<Windows网络编程> 这本书,把里面提到的每种IO模型都试了一次,强烈推荐学习网络编程的同学 ...
随机推荐
- python之MySQL学习——简单的增删改查封装
1.增删改查封装类MysqlHelper.py import pymysql as ps class MysqlHelper: def __init__(self, host, user, passw ...
- ASP.NET Identity 2集成到MVC5项目--笔记02
ASP.NET Identity 2集成到MVC5项目--笔记01 ASP.NET Identity 2集成到MVC5项目--笔记02 继上一篇,本篇主要是实现邮件.用户名登陆和登陆前邮件认证. 1. ...
- 转!!Java虚拟机堆的内存分配和回收
Java内存分配和回收,主要就是指java堆的内存分配和回收.java堆一般分为2个大的区域,一块是新生代,一块是老年代.在新生代中又划分了3块区域,一块eden区域,两块surviver区域.一般称 ...
- Tensorflow神经网络进行fiting function
使用Tensorflow中的神经网络来拟合函数(y = x ^ 3 + 0.7) # -*- coding:utf-8 -*-import tensorflow as tf import numpy ...
- linux查看某个端口被哪个程序占用
查看某个端口被哪个程序占用 netstat -anp |grep 端口号 查看进程号对应的程序 ps -ef | grep 17997 查看指定端口号的进程情况 netstat -tunlp
- 算法:LRU(最近最少使用)
算法:LRU(最近最少使用) 本文参考自小灰文章:https://mp.weixin.qq.com/s/B5xiVeW22ZumbI9KfrYJSg LRU算法 什么是LRU算法 LRU算法又称最近最 ...
- idea 快键件大全
最常用快捷键1.Ctrl+E,可以显示最近编辑的文件列表2.Shift+Click可以关闭文件3.Ctrl+[或]可以跳到大括号的开头结尾4.Ctrl+Shift+Backspace可以跳转到上次编辑 ...
- 查询前几条记录 top limit
SQL Server 数据库中的Top关键字可实现查询数据库表中的前几条数据,但是需要注意的是,Top关键字只能在SQL Server数据库中可以使用,而在MySQL数据库中就要使用具有同样功能的LI ...
- exports与module.exports的区别
nodejs有自己的模块系统,分为文件模块和内置模块.webpack是运行在node环境中,在学习vue-cli的webpack配置的时候, 发现有的文件模块: exports.fun1=functi ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...