1、pom.xml依賴添加
<!-- 導入excel -->
<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();
}