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  [相關(guān)類/方法] 
           * 
          @since  [產(chǎn)品/模塊版本] 
           
          */  
          @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 閱讀(2633) 評論(2)  編輯  收藏 所屬分類: SPRING MVC

          Feedback

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

          下載大文件的時候會內(nèi)存溢出  回復(fù)  更多評論   

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

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


          主站蜘蛛池模板: 安塞县| 鲜城| 高密市| 醴陵市| 海南省| 维西| 雷山县| 古交市| 开江县| 汽车| 兴隆县| 滨州市| 茌平县| 富民县| 红河县| 华阴市| 海南省| 普定县| 禹州市| 通化市| 象山县| 嘉兴市| SHOW| 五原县| 山阳县| 岳阳县| 铜山县| 咸阳市| 大余县| 措美县| 洪江市| 无极县| 眉山市| 西城区| 枞阳县| 周口市| 茂名市| 察雅县| 双柏县| 边坝县| 天柱县|