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图片的上传与下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PopUpWindow使用详解(二)——
- 下一篇: 【BZOJ 4057 Kingdoms】