javaWeb文件的上传和下载(-) 文件的下载
jsp的代码部分
<a href="${pageContext.request.contextPath}/fileDownload?fileName=哈哈.rar">压缩包
</a>
servlet的代码部分
String fileName = request.getParameter(
"fileName");
fileName =
new String(fileName.getBytes(
"iso-8859-1"),
"utf-8");
response.setContentType(
this.getServletContext().getMimeType(fileName));
response.setHeader(
"content-disposition",
"attachment;fileName="+fileName);
String path =
this.getServletContext().getRealPath(
"/resources");
File file =
new File(path,fileName);
InputStream
in =
new FileInputStream(file);
OutputStream
out = response.getOutputStream();
int b;
while((b=
in.read()) != -
1 ){
write(b);
}
in.close();
out.close();
转载请注明原文地址: https://ju.6miu.com/read-37368.html