javaWeb页面实现下载

jsp页面发送请求,后台实现指定路径下的文件,在浏览器上完成下载功能。
@RequestMapping(value = "downloadFile")
public void downloadFile(HttpServletResponse response) throws Exception {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream is = null;
String zipFileName = "test.zip";
String zipFilePath = "D:\\";
File file = new File(zipFilePath + zipFileName);
try {
is = new FileInputStream(file);
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader(
"Content-disposition",
"attachment; filename="
+ new String(zipFileName.getBytes("GBK"),
"ISO8859-1"));
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/html");
response.getWriter().print("download failed");
} finally {
try {
if (is != null)
is.close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

转载于:https://www.cnblogs.com/xubb/p/8487189.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件上传和下载是Web应用程序中常见的功能之一,下面给出一个简单的JavaWeb实现: 1. 文件上传 在JSP页面中,需要一个表单,用于选择文件并提交: ```html <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form> ``` 在Servlet中,处理文件上传的代码如下: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取上传的文件 Part filePart = request.getPart("file"); String fileName = getFileName(filePart); // 保存上传的文件 OutputStream out = new FileOutputStream(new File("D:/uploads/" + fileName)); InputStream in = filePart.getInputStream(); byte[] buffer = new byte[1024]; int length = -1; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } in.close(); out.close(); // 返回上传成功的信息 response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.println("上传成功!"); } // 获取上传的文件名 private String getFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); String[] tokens = contentDisposition.split(";"); for (String token : tokens) { if (token.trim().startsWith("filename")) { return token.substring(token.indexOf("=") + 2, token.length() - 1); } } return ""; } ``` 2. 文件下载 在JSP页面中,需要一个超链接,用于触发文件下载: ```html <a href="download?fileName=test.txt">Download</a> ``` 在Servlet中,处理文件下载的代码如下: ```java protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取要下载的文件名 String fileName = request.getParameter("fileName"); // 设置响应头,告诉浏览器下载文件 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1")); // 读取要下载的文件并写入响应流 InputStream in = new FileInputStream(new File("D:/uploads/" + fileName)); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int length = -1; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } in.close(); out.close(); } ``` 以上就是JavaWeb实现文件上传下载的简单示例,需要注意的是上传的文件保存在服务器的硬盘上,因此需要确保上传的文件不会对服务器造成安全威胁,如上传病毒文件、木马等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值