paulwong

          springMVC 文件下載

          import java.io.File;  
          import java.io.IOException;  
            
          import org.apache.commons.io.FileUtils;  
          import org.springframework.context.annotation.Scope;  
          import org.springframework.http.HttpHeaders;  
          import org.springframework.http.HttpStatus;  
          import org.springframework.http.MediaType;  
          import org.springframework.http.ResponseEntity;  
          import org.springframework.stereotype.Component;  
          import org.springframework.web.bind.annotation.RequestMapping;  
            
          /** 
           * <一句話功能簡述> 
           * <功能詳細描述> 
           *  
           * 
          @author  Administrator 
           * 
          @version  [版本號, 2014年3月7日] 
           * 
          @see  [相關類/方法] 
           * 
          @since  [產品/模塊版本] 
           
          */  
          @Component  
          @Scope("prototype")   
          @RequestMapping("/downloadFile")  
          public class DownloadAction  
          {  
            
              @RequestMapping("download")    
              public ResponseEntity<byte[]> download() throws IOException {    
                  String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\圖片10(定價后).xlsx";  
                  File file=new File(path);  
                  HttpHeaders headers = new HttpHeaders();    
                  String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//為了解決中文名稱亂碼問題  
                  headers.setContentDispositionFormData("attachment", fileName);   
                  headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
                  return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                                    headers, HttpStatus.CREATED);    
              }    
          }  


          JSP
          <href="./downloadFile/download" >下載</a>  

          posted on 2014-10-29 17:17 paulwong 閱讀(2627) 評論(2)  編輯  收藏 所屬分類: SPRING MVC

          Feedback

          # re: springMVC 文件下載[未登錄] 2015-06-01 11:34 jimmy

          下載大文件的時候會內存溢出  回復  更多評論   

          # re: springMVC 文件下載 2015-06-19 10:05 zuidaima

          spring mvc demo教程源代碼下載:http://zuidaima.com/share/kspringmvc-p1-s1.htm  回復  更多評論   


          主站蜘蛛池模板: 海城市| 井冈山市| 汪清县| 宝鸡市| 滨海县| 额济纳旗| 商水县| 武清区| 南岸区| 岳阳县| 囊谦县| 和硕县| 鄂尔多斯市| 萝北县| 平江县| 新干县| 农安县| 呼图壁县| 鄂尔多斯市| 大丰市| 西藏| 施秉县| 仁化县| 蒙阴县| 全椒县| 彩票| 光山县| 古浪县| 瑞安市| 静宁县| 肇东市| 株洲市| 论坛| 白城市| 星子县| 永福县| 宁陕县| 大兴区| 乌恰县| 宁武县| 无极县|