springMvc文件下载

 

//主要看导入的是那些类

import com.ibm.db.service.ITopicService;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

 

@RequestMapping("/down")
public ResponseEntity<String> download() throws IOException {
String path="D:\\upload\\file\\20160419211657.txt";//需要下载的文件
File file=new File(path);
HttpHeaders headers = new HttpHeaders();
String fileName=new String("你好.txt".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
/* String sendString=new String( bytes , "ISO-8859-1" );*/
return new ResponseEntity<String>(new String(FileUtils.readFileToByteArray(file),"ISO-8859-1"),
headers, HttpStatus.CREATED);
}

posted @ 2016-04-21 21:57  zouminglan  阅读(221)  评论(0编辑  收藏  举报