簡單的上傳文件,不約束上傳文件的類型,均可進行上傳操作.
尤其是前後端前後端代碼分離的時候,前後端代碼的聯調很讓人奔潰的,所以將前臺代碼也貼出來了..供大家使用...
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="./jquery-1.11.1.min.js"></script>
<title></title>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data">
文件:<input id="file" type="file" name="與後臺接收一致"/>
</form>
<button id="upload">上傳文件</button>
</body>
<script type="text/javascript">
$(function () {
$("#upload").click(function () {
var formData = new FormData($('#uploadForm')[0]);
$.ajax({
type: 'post',
url: "http://localhost:18010/file/fileUpload",
data: formData,
cache: false,
processData: false,
contentType: false,
success:function (data) {
alert(data);
},
error:function () {
alert("上傳失敗");
}
});
});
});
</script>
</html>
可是js文件還是沒有辦法上傳,所以更尷尬....(js就真的不傳了..)
後臺暴露接口爲:
package com.young.springdemo.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
//文件上傳和下載接口
@RestController
@RequestMapping(value = "file")
@CrossOrigin
public class FileUploadController {
@RequestMapping(value = "fileUpload",method = RequestMethod.POST)
public String fileUpload(@RequestParam("formData") MultipartFile file){
if(file.isEmpty()){
return "false";
}
String fileName = file.getOriginalFilename();
int size = (int) file.getSize();
System.out.println(fileName + "-->" + size);
String path = "E:/test" ;
File dest = new File(path + "/" + fileName);
if(!dest.getParentFile().exists()){ //判斷文件父目錄是否存在
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest); //保存文件
return "true";
} catch (IllegalStateException e) {
e.printStackTrace();
return "false";
} catch (IOException e) {
return "false";
}
}
@RequestMapping(name = "/download",method = RequestMethod.GET)
public String downLoad(HttpServletResponse response,@RequestParam("path")String path) throws UnsupportedEncodingException {
String filename="nacos_config_export_2020-01-02 21_47_46.zip";
String filePath = "E:/test" ;
File file = new File(filePath + "/" + filename);
System.out.println("下載"+filename+"路徑="+path);
if(file.exists()){ //判斷文件父目錄是否存在
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
// response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename,"UTF-8"));
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件輸入流
BufferedInputStream bis = null;
OutputStream os = null; //輸出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download---" + filename);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
其他的具體配置,文件路徑或者上傳文件大小限制什麼之類的,可以後期自己慢慢維護....
所以該Controller在swagger中是可以暴露的,且正常上傳沒問題...然後該html文件,在chrome中執行也沒問題的...所以接口都是沒問題的.....
但是在項目使用過程中,突然出現一個點擊調用該接口,然後session就丟了,客戶直接退出頁面了....
這是正常現象,可以到權限的地方,把該應用的filter過濾掉,直接進行...(這個可以考慮get方式的請求處理)