package com.clj.test.down.util; 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); } }
在瀏覽器裏面寫一句,即可如下載文件html
<a href="./downloadFile/download" >下載</a>
springMVC具體怎麼配置,請參看:java
http://blog.csdn.net/clj198606061111/article/details/20492887
web