日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java jsp filename filepath 图片上传_SpringMVC实现文件上传与下载

發布時間:2025/3/19 javascript 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jsp filename filepath 图片上传_SpringMVC实现文件上传与下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

單文件上傳:

pom.xml:

commons-io

commons-io

1.3.2

commons-fileupload

commons-fileupload

1.2.1

jstl

jstl

1.2

taglibs

standard

1.1.2

upload.jsp:

1.input的type設置為file。

2.form表單的method設置為post。(get請求只會將文件名傳給后臺)

3.form表單的enctype設置為multipart/form-data,以二進制的形式傳輸數據。

" method="post" enctype="multipart/form-data">

上傳的圖片

FileController.java:

@RequestMapping(value="upload", method = RequestMethod.POST)

public String upload(@RequestParam(value="img")MultipartFile img, HttpServletRequest request)

throws Exception {

//getSize()方法獲取文件的大小來判斷是否有上傳文件

if (img.getSize() > 0) {

//獲取保存上傳文件的file文件夾絕對路徑

String path = request.getSession().getServletContext().getRealPath("file");

//獲取上傳文件名

String fileName = img.getOriginalFilename();

File file = new File(path, fileName);

img.transferTo(file);

//保存上傳之后的文件路徑

request.setAttribute("filePath", "file/"+fileName);

System.out.println("file/"+fileName);

return "upload";

}

return "error";

}

springMVC.xml:

注意:這里需要在webapp文件下手動創建一個file文件夾。

多文件上傳:

uploads.jsp:

file1:

file2:

file3:

上傳的圖片

FileController.java:

@RequestMapping(value="/uploads", method = RequestMethod.POST)

public String uploads(@RequestParam MultipartFile[] imgs, HttpServletRequest request)throws Exception {

//創建集合,保存上傳后的文件路徑

ListfilePaths = new ArrayList();

for (MultipartFile img : imgs) {

if (img.getSize() > 0) {

String path = request.getSession().getServletContext().getRealPath("file");

String fileName = img.getOriginalFilename();

File file = new File(path, fileName);

filePaths.add("file/"+fileName);

img.transferTo(file);

}

}

request.setAttribute("filePaths", filePaths);

return "uploads";

}

文件下載:

download.jsp:

Insert title here

下載圖片

FileController.java:

@RequestMapping("/download")

public void downloadFile(String fileName,HttpServletRequest request,

HttpServletResponse response){

if(fileName!=null){

//獲取file絕對路徑

String realPath = request.getServletContext().getRealPath("file/");

File file = new File(realPath,fileName);

OutputStream out = null;

if(file.exists()){

//設置下載完畢不打開文件

response.setContentType("application/force-download");

//設置文件名

response.setHeader("Content-Disposition", "attachment;filename="+fileName);

try {

out = response.getOutputStream();

out.write(FileUtils.readFileToByteArray(file));

out.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(out != null){

try {

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

}

總結

以上是生活随笔為你收集整理的java jsp filename filepath 图片上传_SpringMVC实现文件上传与下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。