文件下载 悠悠 2021-12-14 12:55 368阅读 0赞 1,struts2文件下载: 1)Action配置 // 下载, public String downloadFile() throws Exception \{ return SUCCESS; \} // 文件下载(读取文件流的入口,必须返回一个文件输入流) public InputStream getDownloadFile() throws FileNotFoundException \{ System.out.println(riskEvent.getFilePath()); // 图片路径 String fileName =basePath +"/"+ riskEvent.getFilePath(); // 如果下载文件名为中文,进行字符编码转换 getHttpResponse().setHeader("Content-Disposition","attachment;fileName="+riskEvent.getFilePath()); InputStream inputStream = new FileInputStream(fileName); System.out.println(inputStream); return inputStream; \} 2)xml的配置 <action name="downloadFile" class="riskSurveyAction" method="downloadFile" > <result name="success" type="stream"> <param name="contentDisposition"> attachment;filename="$\{riskEvent.filePath\}" </param> <param name="inputName">downloadFile</param> <param name="bufferSize">2048</param> </result> </action> 2,自己写的下载 /\*\* \* 下载小票 \* @throws IOException \*/ public void downLoadFile() throws IOException\{ String fileName = riskEvent.getFilePath(); // 图片路径 String filePath = basePath +"/"+ fileName.substring(0, fileName.lastIndexOf(".")) + "/"+fileName; HttpServletResponse response = getHttpResponse(); File file = new File(filePath); long len = file.length(); down(file, fileName, response); \} public void down(File f,String filename,HttpServletResponse response) \{ response.reset(); response.setHeader("content-disposition","attachment; filename="+filename); //设置下载的文件名 long fileLength=f.length(); String length1=String.valueOf(fileLength); response.setHeader("Content\_Length",length1); //下载文件的大小 InputStream in=null; OutputStream out = null; try\{ in = new FileInputStream( f ); out = response.getOutputStream(); byte\[\] buffer = new byte\[2097152\]; int ins = in.read(buffer);//读取字节到buffer中 //ins == -1 时 。就已经是文件的结尾了 while ( ins != -1 ) \{ out.write(buffer, 0, ins);//将缓存buffer中的数据写到文件中 ins = in.read(buffer); \} in.close(); out.flush(); out.close(); \}catch (Exception e) \{ System.out.println("--下载发生异常--"); try \{ in.close(); out.flush(); out.close(); \} catch (IOException e1) \{ System.out.println("--关闭发生异常--"); in = null; out = null; e1.printStackTrace(); \} \} \} 转载于:https://www.cnblogs.com/siashan/p/4276510.html
相关 文件下载 String filePath = wenjianService.getlujing(attachId); String fileName= wenjian 川长思鸟来/ 2022年08月23日 00:52/ 0 赞/ 71 阅读
相关 文件下载 下载图片或者文件有那么几种方法,下面详细总结。 1,js方法 function downloadFile(url){ var elemIF Bertha 。/ 2022年08月06日 15:29/ 0 赞/ 177 阅读
相关 文件下载 对应文件上传的下载 1、javaweb方法实现: public String download(){ try { //获取文件ID ゝ一世哀愁。/ 2022年07月13日 14:08/ 0 赞/ 288 阅读
相关 下载文件 //设置文件MIME类型 response.setContentType(getServletContext().getMimeType(f 浅浅的花香味﹌/ 2022年06月17日 05:44/ 0 赞/ 257 阅读
相关 文件下载 下载就是向客户端响应字节数据,原来响应的都是html的字符数据,现在 把一个文件变成字节数组,使用response.getOutputStream()来各应给浏览器! 下载 - 日理万妓/ 2022年06月10日 03:10/ 0 赞/ 208 阅读
相关 文件下载 导入jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 在jsp中添加超链接,设置要下载的文件 叁歲伎倆/ 2022年05月06日 10:44/ 0 赞/ 277 阅读
相关 文件下载 html 标签 点击自动下载 <button> <a href = "http://localhost/day.zip"> 布满荆棘的人生/ 2022年03月18日 13:56/ 0 赞/ 641 阅读
相关 下载文件 import javax.servlet.http.HttpServletResponse; import java.io.BufferedOutputStre 骑猪看日落/ 2022年01月11日 02:57/ 0 赞/ 334 阅读
相关 文件下载 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ub 川长思鸟来/ 2021年12月15日 00:53/ 0 赞/ 409 阅读
还没有评论,来说两句吧...