springCloud+sprigMVCファイルのアップロード制限問題
3819 ワード
http:
multipart:
enabled: true # http
max-file-size: 100MB #
max-request-size: 100MB #
file-size-threshold: 1MB # 1MB
location: / #
multipart制限の説明を増加します.-----------------------------------------------
web端末
var vm = new Vue({
el : "#app",
data : {
},
mounted : function(){
},
methods : {
//
onUpload(e){
var formData = new FormData();
formData.append('file', e.target.files[0]);
formData.append('type', 'test');
$.ajax({
url: $$pageContextPath +'/upload',
type: 'POST',
dataType: 'json',
cache: false,
data: formData,
processData: false,
contentType: false,
success: (res) => {
console.log(res);
if (res.code === 200) {
console.log(res);
}
},
error: function(err) {
console.log(err);
}
});
}
}
});
//
package com.xxxx
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UploadFileDemoController {
/**
* ,
* @param dc
* @return
*/
@RequestMapping({ "/uploadinit" })
public String init() {
return "/uploadFileDemo.html";
}
@RequestMapping(value="/upload")
@ResponseBody
public String retrieve(HttpServletRequest request,
@RequestParam("description") String description,
@RequestParam("file") MultipartFile file) {
System.out.println(" :"+description);
// ,
if(!file.isEmpty()) {
//
String filename = file.getOriginalFilename();
System.out.println(filename);
try {
InputStream in = file.getInputStream();
// G
File toFile = new File("G:\\",filename);
FileOutputStream out = new FileOutputStream(toFile);
byte[] b = new byte[1024];
int a = 0;
while((a = in.read(b))!=-1){
out.write(b, 0, a);
}
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
} else {
return "-1";
}
}
}