springmvc实现文件下载到Android手机设备pda端

1:首先要添加相关得jar文件,在pom.xml中

1     <dependency>  
2         <groupId>commons-fileupload</groupId>  
3         <artifactId>commons-fileupload</artifactId>  
4         <version>1.3</version>      
5     </dependency>

2:定义文件解析器

1 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
2         <!-- 设置默认编码 -->  
3         <property name="defaultEncoding" value="utf-8"></property>  
4         <!-- 上传图片最大大小5M-->   
5         <property name="maxUploadSize" value="5242440"></property>    
6 </bean> 

3:jsp页面

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>文件下载</title>
 8     <style>
 9         .down {
10             text-align:center;
11             font-size:150px;
12         }
13     </style>
14 </head>
15 <body>
16     <div class="down">
17     <a href="${pageContext.request.contextPath }/testDownload">前往下载</a><br/>
18     </div>
19 </body>
20 </html>

4:Controller层

 1     @RequestMapping(value="/testDownload", produces="application/vnd.android.package-archive")
 2     public ResponseEntity<byte[]>  testDownload(HttpServletRequest request,HttpServletResponse resp){
 3         String filename="ymhscn.apk";
 4         ServletContext scontext=request.getServletContext();
 5         String path=scontext.getRealPath("/WEB-INF/"+filename);
 6         System.out.println(path);
 7         File f=new File(path);
 8         InputStream in =null;
 9         ResponseEntity<byte[]> response=null ;
10         try {
11             in = new FileInputStream(f);
12             byte[] b=new byte[in.available()];
13             in.read(b);
14             HttpHeaders headers = new HttpHeaders();
15             filename = new String(filename.getBytes(),"utf-8");
16             //MediaType mt = new MediaType("application/vnd.android.package-archive");
17             //headers.setContentType(mt);
18             //以下的类型必须要设置,要不不能在android机上正常下载的   
19             headers.add("Content-Disposition", "attachment;filename="+filename);
20             
21             //resp.setContentType("application/vnd.android.package-archive");
22             HttpStatus statusCode=HttpStatus.OK;
23             response = new ResponseEntity<byte[]>(b, headers, statusCode);  
24             
25         } catch (FileNotFoundException e) {
26             e.printStackTrace();
27         } catch (IOException e) {
28             e.printStackTrace();
29         }finally{
30             try {
31                 in.close();
32             } catch (IOException e) {
33                 e.printStackTrace();
34             }
35             
36         }
37         return response;
38     }

5:注意!!!!springmvc 特定mime类型返回,想要Android默认浏览器下载成功,一定要在这里加这一句

原文地址:https://www.cnblogs.com/ccEmma/p/8336195.html