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);
}
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);
}
相关文章推荐
- java安全-类加载器
- LeetCode 217 Contains Duplicate II java
- Java 进行 RSA 加解密时不得不考虑到的那些事儿
- java获取MySQL自动的int类型的Id
- 20145238-荆玉茗 《Java程序设计》第8周学习总结
- 深入理解Java:注解(Annotation)自定义注解入门
- LeetCode 127 Word Ladder java (看某位大神的解法做出来的)
- Java split()方法使用注意问题
- Java发送带附件邮件示例程序
- 命令行下运行 java someClass.class出现 “错误:找不到或无法加载主类someClass ” 的解决方案
- Java的Properties类
- Java中的线程状态之终止
- eclipse配置red5服务器
- java 读取配置文件的几种方法
- solr源码导入eclipse中
- Java基础知识
- spring 动态数据源
- spring websocket 利用注解接收和发送消息
- 通过web.xml文件自动启动spring容器加载对应的配置文件
- 解决java问题