Web Uploader圖片上傳 java實現
阿新 • • 發佈:2019-02-20
後臺程式碼
@RequestMapping("/fileUpload.do")
public void uploadFiles(@RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response){
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
String realPath = request.getSession ().getServletContext().getRealPath("/WEB-INF/upload/");
String originalFilename = file.getOriginalFilename();
String pathname = realPath + "\\"+originalFilename;
FileUtils.copyInputStreamToFile(inputStream,new File(pathname));
System.out.println("sucess !");
} catch (IOException e) {
e.printStackTrace ();
}
try {
response.getWriter().write("{\"code\":200}");
} catch (IOException e) {
e.printStackTrace();
}
}
upload.js檔案中需要修改的地方
uploader = WebUploader.create({
pick: {
id: ‘#filePicker’,
label: ‘點選選擇圖片’
},
formData: {
uid: 123
},
dnd: ‘#dndArea’,
paste: ‘#uploader’,
swf: ‘image-upload/Uploader.swf’,
chunked: false,
chunkSize: 512 * 1024,
server: ‘/fileUpload.do’,//改成自己伺服器的連結
// runtimeOrder: ‘flash’,
// accept: {
// title: 'Images',
// extensions: 'gif,jpg,jpeg,bmp,png',
// mimeTypes: 'image/*'
// },
// 禁掉全域性的拖拽功能。這樣不會出現圖片拖進頁面的時候,把圖片開啟。
disableGlobalDnd: true,
fileNumLimit: 300,
fileSizeLimit: 200 * 1024 * 1024, // 200 M
fileSingleSizeLimit: 50 * 1024 * 1024 // 50 M
});