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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springMVC图片的上传与下载

發布時間:2025/4/9 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springMVC图片的上传与下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1*上傳的方法:

// 從客戶端上傳數據到服務器(客戶端→(讀入)程序→(寫出)服務器)
@RequestMapping("/up")
public String upLoad(@RequestParam MultipartFile myFile, HttpServletRequest request) throws Exception {

// 創建I流--圖片文件myFile轉化為流--讀入程序!
InputStream inputStream = myFile.getInputStream();
// 改為uuid名!
String newFileName = getNewName(myFile);
System.out.println(newFileName);
// 查找即將上傳到服務器中的真實路徑!
String realPath = request.getServletContext().getRealPath("/img");
File file = new File(realPath);
if (!file.exists()) {
file.mkdirs();
}
System.out.println(realPath);
// 程序 寫出 上傳服務器!
FileOutputStream fileOutputStream = new FileOutputStream(realPath + "\\" + newFileName);
// 復制多功能文件(圖片)以及關閉流
IOUtils.copy(inputStream, fileOutputStream);

inputStream.close();
fileOutputStream.close();
// 將文件放入request域中在頁面上用EL獲取--(均采用絕對路徑,避免出錯!)
request.setAttribute("imgPath", "/imageUpload/img/" + newFileName);

return "/index.jsp";
}

?

?

?

2*生成uuid名字的方法

public String getNewName(MultipartFile file) {
// 找到原始文件名
String originalFilename = file.getOriginalFilename();
// 找到后綴名.的位置
int lastIndexOf = originalFilename.lastIndexOf(".");
// 截取后綴名
String substring = originalFilename.substring(lastIndexOf);
// 生成uuid
String uuid = UUID.randomUUID().toString();
String newName = uuid + substring;

return newName;

}

?

?

?

3*下載的方法

@RequestMapping("/down")
public void down(String fileName, HttpServletResponse response, HttpServletRequest request) throws Exception {

// 設置響應頭:內容處理方式 → attachment(附件,有為下載,沒有為預覽加載) →指定文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
// 從服務器上下載圖片,要找到圖片在服務器中的真實位置
String realPath = request.getServletContext().getRealPath("/img");
// 從服務器上讀入程序中
InputStream fileInputStream = new FileInputStream(realPath + "\\" + fileName);
// 從程序中寫出下載到客戶端
OutputStream outputStream = response.getOutputStream();
// copy以及關閉流資源
IOUtils.copy(fileInputStream, outputStream);
outputStream.close();
fileInputStream.close();

}

2017-10-18

附錄:spring.xml+web.xml

?

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

?

<!-- 配一個control的掃描器 -->
<context:component-scan base-package="com.zy.control"></context:component-scan>

?

<!-- 配置上傳文件解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10240000</value><!-- 最大上傳尺寸:102400B -->
</property>

?

</bean>

?

web.xml

?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SpringMVC_imageUpload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 前端控制器 -->
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:spring.xml</param-value>
</init-param>
</servlet>
<!-- 映射 -->
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- 編碼過濾器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

?

轉載于:https://www.cnblogs.com/weixc/p/7689529.html

總結

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

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