您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“java文件流接口和下載的方法是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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); /** * 將直接資源請(qǐng)求接口地址轉(zhuǎn)為文件流接口 * 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=文件二進(jìn)制對(duì)象 // 獲取時(shí)間戳 var timestamp=new Date().getTime(); // 獲取XMLHttpRequest var xmlResquest = new XMLHttpRequest(); // 發(fā)起請(qǐng)求 xmlResquest.open("POST", "http://localhost:8086/rainmanqqst/api/system/resource/aHR0cHM6Ly9pbWcxLmRvdWJhbmlvLmNvbS92aWV3L3Bob3RvL2wvcHVibGljL3AxNDIyMDcxNjA4LndlYnA=", true); // 設(shè)置請(qǐng)求頭類型 xmlResquest.setRequestHeader("Content-type", "application/json"); // 設(shè)置請(qǐng)求token //xmlResquest.setRequestHeader( // "Authorization", // Vue.cookie.get('token') //); xmlResquest.responseType = "blob"; // 返回 xmlResquest.onload = function(oEvent) { var content = xmlResquest.response; alert(content); // 組裝a標(biāo)簽 var elink = document.createElement("a"); // 設(shè)置下載文件名 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文件流接口和下載的方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。