jsp实现文件下载的两种方式,jsp实现文件的上传和下载
更新:2022-11-20 08:58
本文目录一览:
jsp中mp3文件下载要怎么实现?
有很多种方式:
- 直接使用
request.sendRedirect("music/test.mp3");
- 如果你想隐藏路径,可以使用:
RequestDispatcher rd = sc.getRequestDispatcher("/music/test.mp3").forward(request, response);
- 或者直接用流的方式返回给用户。
如果返回的 MP3 文件在浏览器中直接打开出现乱码,你需要在
web.xml
中添加 MIME 映射:
<mime-mapping>
<extension>mp3</extension>
<mime-type>audio/x-mpeg</mime-type>
</mime-mapping>
jsp 下载文件路径问题
下载文件有两种方式:
- 服务器上能相对找到的文件
比如服务器上的目录是D:\web
,那么文件应该放在D:\web\downfile\111.xls
,超链接可以这样写:<a href="/downfile/111.xls" download>下载文件</a>
- 使用流的方式下载
超链接可以这样写:
后台处理逻辑可以读取<a href="#" onclick="downloadFile()">下载文件</a>
D:\111.xls
文件,并通过response.getOutputStream()
写入响应流,实现下载。
jsp实现文件的下载
<%@ page language="java" import="java.io.*,java.net.*" contentType="application/x-msdownload" pageEncoding="UTF-8"%>
<%
String url = request.getParameter("url");
int k = url.lastIndexOf("\\");
String url1 = url.substring(k + 1, url.length());
response.reset();
response.setContentType("application/x-download");
String filedownload = url;
String filedisplay = url1;
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
out.clear();
out = pageContext.pushBody();
outp.flush();
} catch (Exception e) {
System.out.println("Error!");
} finally {
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
}
}
%>
jsp页面如何实现下载文档
在 JSP 页面中,通常使用 <a>
标签实现文件下载。当用户点击该链接时,浏览器会弹出保存对话框。
前端 JSP 示例:
<a href="#" onclick="javascript:downloadtest('${app.id})" id="pluginurl" style="color: #83AFE2;text-decoration:underline;">下载文件</a>
JavaScript 示例:
function downloadtest(id) {
var url = "<%=request.getContextPath()%>/app/download/" + id;
$("#pluginurl").attr("href", url);
}
后台 Java 示例:
@RequestMapping(value = "/download/{id}")
public void download(@PathVariable String id, HttpServletResponse response) {
String filepath = "";
Result result = appService.getAppById(id);
App app = (App) result.getMap().get("app");
if (app == null) {
return;
}
filepath = app.getUrl();
File file = new File(filepath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b = new byte[1024];
int len = 0;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType("application/force-download");
String filename = file.getName();
filename = filename.substring(36, filename.length());
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentLength((int) file.length());
while ((len = inputStream.read(b)) != -1) {
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
jsp 如何实现文件上传和下载功能?
文件上传示例:
MyjspForm mf = (MyjspForm) form;
FormFile fname = mf.getFname();
byte[] fn = fname.getFileData();
OutputStream out = new FileOutputStream("D:\\" + fname.getFileName());
Date date = new Date();
String title = fname.getFileName();
String url = "d:\\" + fname.getFileName();
Upload ul = new Upload();
ul.setDate(date);
ul.setTitle(title);
ul.setUrl(url);
UploadDAO uld = new UploadDAO();
uld.save(ul);
out.write(fn);
out.close();
文件下载示例:
DownloadForm downloadForm = (DownloadForm) form;
String fname = request.getParameter("furl");
FileInputStream fi = new FileInputStream(fname);
byte[] bt = new byte[fi.available()];
fi.read(bt);
response.setContentType("application/msdownload;charset=gbk");
String contentDisposition = "attachment; filename=" + "java.txt";
response.setHeader("Content-Disposition", contentDisposition);
response.setContentLength(bt.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(bt);
jsp如何实现文件上传与下载?
如果服务器端程序使用的是 Struts2 框架,可以按照以下方式实现:
文件上传:
JSP 页面只需包含一个 file
类型的表单域:
<input type="file" name="xxx" />
在 Struts2 的 Action 中,定义以下属性并提供相应的 set/get
方法:
File xxx
String xxxFileName
String xxxContentType
框架会自动解析上传的文件流、文件名和类型。
文件下载:
在 Action 的配置文件中添加如下配置:
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=xxx</param>
</result>
在 Action 中编写一个返回 InputStream
的 getInputStream()
方法,用于返回要下载的文件流。
注意:
contentDisposition
中的attachment
表示浏览器会弹出保存对话框;如果设置为inline
,则浏览器会尝试直接打开文件。