Insert title here

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>

 

posted @ 2017-10-18 22:39  别杀我祭天  阅读(1201)  评论(0)    收藏  举报
Insert title here