java 写个controller下载文件(word);两种方式
java 读取某路径下的文件直接相应到前端页面!
前端请求方式为get请求,只需要写个个按标签点击即可下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.harry.dandelion.framework.common.utils.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.sinosoft.ssi.base.entity.ReportEntity; import com.sinosoft.ssi.base.mappers.ReportMapper; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController public class ReportOutWord { @Autowired private ReportMapper RrportMapper; @GetMapping ( "/exp/outWord/1" ) public void download(HttpServletRequest request, HttpServletResponse response) { String path= " " ; // 开始下载文件 try { // path是指欲下载的文件的路径。 File file = new File(path); // 取得文件名。 String filename = file.getName(); // 取得文件的后缀名。 String ext = filename.substring(filename.lastIndexOf( "." ) + 1 ).toUpperCase(); log.debug( "下载文件类型为:" + ext); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream( new FileInputStream(path)); byte [] buffer = new byte [fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header //ISO-8859-1可以显示中文的文件名 response.addHeader( "Content-Disposition" , "attachment;filename=" + new String(filename.getBytes(), "ISO-8859-1" )); response.addHeader( "Content-Length" , "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType( "application/octet-stream" ); toClient.write(buffer); toClient.flush(); toClient.close(); // 删除临时文件 //FileUtils.delFile(rootPath + File.separator + filename); // TODO Auto-generated catch block } catch (IOException ex) { ex.printStackTrace(); } } } |
方式二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // 开始下载文件 InputStream in = null ; try { // 1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType( "multipart/form-data" ); // 2.设置文件头:最后一个参数是设置下载文件名 response.addHeader( "Content-Disposition" , "attachment;filename=" + new String( "*******.docx" .getBytes(), "ISO-8859-1" )); in = new FileInputStream( new File(文件路径)); // 3.通过response获取ServletOutputStream对象(out) int b = 0 ; byte [] buffer = new byte [ 512 ]; while (b != - 1 ) { b = in.read(buffer); if (b != - 1 ) { response.getOutputStream().write(buffer, 0 , b); // 4.写到输出流(out)中 } } } catch (Exception e) { } finally { try { if (in != null ) { in.close(); } response.getOutputStream().flush(); } catch (IOException e) { e.printStackTrace(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix