本篇内容介绍了“java文件流接口和下载的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
java文件流接口
package com.rainmanqqst.controller.filestream;
import com.Ostermiller.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @author RAINMANQQST
* @date 2019-07-15 14:52
* @description
*/
@RestController
@RequestMapping("/fille")
public class FileController {
private static Logger logger = LoggerFactory.getLogger(FileController.class);
/**
* 将直接资源请求接口地址转为文件流接口
* filePath为base64加密的文件地址
* @param filePath
* @param request
* @param response
*/
@RequestMapping(value="/resource/{filePath}")
public void achieveResource(@PathVariable("filePath") String filePath, HttpServletRequest request, HttpServletResponse response){
try {
String path = Base64.decode(filePath);
logger.info("resource path is "+path);
File file = new File(filePath);
logger.info("fileName : "+file.getName());
response.setHeader("Content-disposition", "attachment; filename="+file.getName());
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Length",file.length()+"");
InputStream inputStream = new FileInputStream(file);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len=inputStream.read(buffer))>0){
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
inputStream.close();
servletOutputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
JS下载代码
<html>
<head>
<%--<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">--%>
<script type="text/javascript">
function exportFile() { // name=文件名, blob=文件二进制对象
// 获取时间戳
var timestamp=new Date().getTime();
// 获取XMLHttpRequest
var xmlResquest = new XMLHttpRequest();
// 发起请求
xmlResquest.open("POST", "http://localhost:8086/rainmanqqst/api/system/resource/aHR0cHM6Ly9pbWcxLmRvdWJhbmlvLmNvbS92aWV3L3Bob3RvL2wvcHVibGljL3AxNDIyMDcxNjA4LndlYnA=", true);
// 设置请求头类型
xmlResquest.setRequestHeader("Content-type", "application/json");
// 设置请求token
//xmlResquest.setRequestHeader(
// "Authorization",
// Vue.cookie.get('token')
//);
xmlResquest.responseType = "blob";
// 返回
xmlResquest.onload = function(oEvent) {
var content = xmlResquest.response;
alert(content);
// 组装a标签
var elink = document.createElement("a");
// 设置下载文件名
elink.download = timestamp + ".png";
elink.style.display = "none";
var blob = new Blob([content]);
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
xmlResquest.send();
}
</script>
</head>
<body>
<input onclick="javascript:exportFile()" type="button" value="test" >
</body>
</html>
“java文件流接口和下载的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/241670/blog/3077436