@RequestMapping( "/down" )
public ResponseEntity< byte[ ] > down( HttpSession session) throws IOException{
//获取下载文件的路径
String realPath = session.getServletContext( ) .getRealPath( "img" ) ;
String finalPath = realPath + File.separator + "2.jpg" ;
InputStream is = new FileInputStream( finalPath) ;
//available( ) :获取输入流所读取的文件的最大字节数
byte[ ] b = new byte[ is.available( ) ] ;
is.read( b) ;
//设置请求头
HttpHeaders headers = new HttpHeaders( ) ;
headers.add( "Content-Disposition" , "attachment;filename=zzz.jpg" ) ;
//设置响应状态
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity< byte[ ] > entity = new ResponseEntity< byte[ ] > ( b, headers, statusCode) ;
return entity;
}
@RequestMapping( value= "/up_old" , method= RequestMethod.POST)
public String up_old( String desc, MultipartFile uploadFile, HttpSession session) throws IOException {
//获取上传文件的名称
String fileName = uploadFile.getOriginalFilename( ) ;
String path = session.getServletContext( ) .getRealPath( "photo" ) + File.separator + fileName;
//获取输入流
InputStream is = uploadFile.getInputStream( ) ;
//获取输出流
File file = new File( path) ;
OutputStream os = new FileOutputStream( file) ;
/*int i = 0;
while (( i = is.read( )) != -1) {
os.write( i) ;
} */
/*int i = 0;
byte[ ] b = new byte[ 1024] ;
while (( i = is.read( b)) != -1) {
os.write( b, 0, i) ;
} */
os.close( ) ;
is.close( ) ;
return "success" ;
}
@RequestMapping( value= "/up" , method= RequestMethod.POST)
public String up( String desc, MultipartFile uploadFile, HttpSession session) throws IOException {
//获取上传文件的名称
String fileName = uploadFile.getOriginalFilename( ) ;
String finalFileName = UUID.randomUUID( ) + fileName.substring( fileName.lastIndexOf( "." )) ;
String path = session.getServletContext( ) .getRealPath( "photo" ) + File.separator + finalFileName;
File file = new File( path) ;
uploadFile.transferTo( file) ;
return "success" ;
}
/**
*
* @param desc 描述信息( 可有可无)
* @param uploadFile
* @param session
* @return
* @throws IOException
*/
@RequestMapping( value = "/up_multi" , method = RequestMethod.POST)
public String up_multi( String desc, MultipartFile[ ] uploadFile, HttpSession session) throws IOException {
// 获取上传文件的名称
for ( MultipartFile m : uploadFile) {
String fileName = m.getOriginalFilename( ) ;
String finalFileName = UUID.randomUUID( ) + fileName.substring( fileName.lastIndexOf( "." )) ;
String path = session.getServletContext( ) .getRealPath( "photo" ) + File.separator + finalFileName;
File file = new File( path) ;
m.transferTo( file) ;
}
return "success" ;
}
< ?xml version= "1.0" encoding= "UTF-8" ?>
< beans xmlns= "http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
< context:component-scan base-package= "com.atguigu" > < /context:component-scan>
< bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name= "prefix" value= "/WEB-INF/view/" > < /property>
< property name= "suffix" value= ".jsp" > < /property>
< /bean>
< mvc:default-servlet-handler/>
< mvc:annotation-driven />
< ! --
处理文件,将客户端上传的File文件,处理为MultipartFile
注意:文件解析器的bean中id必须设置为multipartResolver
-->
< bean id= "multipartResolver" class= "org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< ! -- 设置文件解析的编码,注意:一定要和页面的pageEncoding保持一致 -->
< property name= "defaultEncoding" value= "UTF-8" > < /property>
< ! -- 设置最大上传文件大小 -->
< property name= "maxUploadSize" value= "88888888" > < /property>
< /bean>
< /beans>
< ?xml version= "1.0" encoding= "UTF-8" ?>
< web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp_ID" version= "2.5" >
< display-name> SpringMVC04< /display-name>
< welcome-file-list>
< welcome-file> index.jsp< /welcome-file>
< /welcome-file-list>
< 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>
< ! -- The front controller of this Spring Web application, responsible for handling all application requests -->
< servlet>
< servlet-name> springDispatcherServlet< /servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< /servlet-class>
< init-param>
< param-name> contextConfigLocation< /param-name>
< param-value> classpath:springMVC.xml< /param-value>
< /init-param>
< load-on-startup> 1< /load-on-startup>
< /servlet>
< ! -- Map all requests to the DispatcherServlet for handling -->
< servlet-mapping>
< servlet-name> springDispatcherServlet< /servlet-name>
< url-pattern> /< /url-pattern>
< /servlet-mapping>
< /web-app>
< %@ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" %>
< ! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html>
< head>
< meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >
< title> Insert title here< /title>
< /head>
< body>
< a href= "down" > 下载图片< /a>
< br/>
< br/>
< form action= "up" method= "post" enctype= "multipart/form-data" >
头像:< input type= "file" name= "uploadFile" />
描述:< input type= "text" name= "desc" />
< input type= "submit" value= "上传" />
< /form>
< br/>
< h3> 批量上传< /h3>
< form action= "up_multi" method= "post" enctype= "multipart/form-data" >
头像1:< input type= "file" name= "uploadFile" />
头像2:< input type= "file" name= "uploadFile" />
头像3:< input type= "file" name= "uploadFile" />
< input type= "submit" value= "上传" />
< /form>
< /body>
< /html>