您的位置:首页 > 编程语言 > Java开发

springMvc文件下载

2016-04-21 21:57 337 查看
//主要看导入的是那些类

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: