spring boot上传文件
1、pom.xml依赖添加
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
2、设置application.properties
上传的文件大小
# Single file max size
spring.http.multipart.max-file-size=100Mb
# All files max size
spring.http.multipart.max-request-size=100Mb
3、html
<form id="uploadForm">
<table>
<tr>
<td>
<input class="btn btn-default" type="file" name="img" multiple="multiple"></input>
</td>
<td>
<input class="btn btn-default" type="button" value="上传" onclick="doUpload()">
<input type="text" id="fileNameList" style="display:none;"></input>
<input type="hidden" name="test"></input><!--在ie11中必须要添加次行-->
</td>
</tr>
</table>
</form>
4、js
//上传文件
function doUpload(){
var formData = new FormData($("#uploadForm")[0]);
$.ajax({
url : "/uploadFileTest",
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success : function(result) {
if(result==""||result==null){
alert("请至少选择一个文件~~");
}else{
var filepathSplit = result.split("ψ");
$("ul li").remove();
for(var f=0;f<filepathSplit.length;f++){
$("#filepath").append("<li style='line-height:170%;'><a style='cursor:pointer;' onclick='officeDemand(\""+filepathSplit[f]+"\")'>"+filepathSplit[f]+"</a></li>");
}
//添加文件删除按钮
if(filepathSplit.length>0){
$("#filepath").append("<li></li><li><a style='cursor:pointer;' onclick='delFile()'>删除</a></li>");
}
$("#filepath").show();
$("#fileNameList").val(result);
alert("上传成功~~");
}
},
error : function(data) {
alert("上传失败~~");
}
});
}
5、java后台
//上传excel,返回文件名称
@RequestMapping("uploadFileTest")
@ResponseBody
public synchronized String uploadFileTest(@RequestParam("img") MultipartFile[] files,HttpServletRequest request){
MultipartFile file;
String fileName = "";
String userid = request.getUserPrincipal().getName();
if(files[0].getOriginalFilename()==null||files[0].getOriginalFilename().length()<=0){
return "";
}else{
for (int i = 0; i < files.length; i++){
String path = "d:/uploadFile";//本地路径(服务器路径)
file = files[i];
fileName = fileName+userid+file.getOriginalFilename()+"ψ";
path = path +"/"+userid+file.getOriginalFilename();
try {
upload(path,file);
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName.substring(0, fileName.length()-1);
}
}
//上传文件
public synchronized void upload(String filename,MultipartFile file) throws IOException{
File outFile = new File(filename);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(outFile)); // 创建文件输出流
//根据上传的文件,创建缓冲区,生成字符流,存入缓冲区
byte[] buffer = file.getBytes();//创建文件流
//把缓冲区的字符流 写入 字符流目的地(把输入流写入输出流)
stream.write(buffer);
//第6步:刷新流,关闭流
stream.close();
}