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  回復  更多評論   


          主站蜘蛛池模板: 新巴尔虎右旗| 西乡县| 东乌珠穆沁旗| 信宜市| 江孜县| 洞头县| 阿巴嘎旗| 囊谦县| 隆回县| 贡觉县| 玉山县| 武穴市| 石嘴山市| 黑山县| 邳州市| 威海市| 青神县| 米易县| 苍溪县| 谷城县| 黑水县| 象州县| 阳西县| 通山县| 虹口区| 天气| 贵阳市| 新乡县| 磴口县| 紫阳县| 江都市| 策勒县| 衡山县| 洛浦县| 辉县市| 琼结县| 孝义市| 始兴县| 麻栗坡县| 宁海县| 砀山县|