资源简介
JavaWeb实现文件上传下载,使用Comm组件,简单实现上传、下载、展示。
代码片段和文件信息
package org.gyt.fileUploadDownload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DownloadServlet
*/
@WebServlet(“/DownloadServlet“)
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String filePath = “404“;
/**
* @see HttpServlet#HttpServlet()
*/
public DownloadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
// TODO Auto-generated method stub
// response.getWriter().append(“Served at: “).append(request.getContextPath());
// 下载文件
System.out.println(“下载文件:“);
// 文件名
String fileName = request.getParameter(“filename“); //文件名_UUID
fileName = new String(fileName.getBytes(“iso8859-1“) “UTF-8“);
// 文件保存在/WEB-INF/file中
String fileStorageRootPath = this.getServletContext().getRealPath(“/WEB-INF/file“);
// 通过文件名找出文件所在目录 返回绝对路径
findFileStoragePathByFileName(fileName new File(fileStorageRootPath));
System.out.println(“Download File: “ + filePath);
// 得到该文件
if (filePath.equals(“404“)) {
request.setAttribute(“message“ “错误“);
request.getRequestDispatcher(“/message.jsp“).forward(request response);
}
File file = new File(filePath);
// 若不存在
if (file != null && !file.exists()) {
request.setAttribute(“message“ “资源已被删除!“);
request.getRequestDispatcher(“/message.jsp“).forward(request response);
return ;
}
// 处理文件名
String realName = fileName.substring(fileName.indexOf(“_“)+1);
// 设置相应头,控制浏览器下载文件
response.setHeader(“content-disposition“ “attachment;filename=“ + URLEncoder.encode(realName “UTF-8“));
// 读取要下载的文件,保存到输入流
FileInputStream inputStream = new FileInputStream(filePath);
// 创建输出流
OutputStream outputStream = response.getOutputStream();
// 创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
while ((len=inputStream.read(buffer)) > 0) {
// 输出缓冲区内容到浏览器,实现下载
outputStream.write(buffer 0 len);
}
// 关闭流
inputStream.close();
outputStream.close();
}
/**
*
* @throws IOException
* @title:findFileStoragePathByFileName
* @Description: 根据文件名找到所在路径 返回绝对路径
* @Param: @param fileName
* @Param: @param fileStorageRootPath
* @Param: @return 返回绝对路径
* @Return: String
*/
private void findFileStoragePathByFileName(String fileName File file) throws IOException {
// TODO Auto-generated method stub
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1418 2018-12-04 23:16 FileUploadDownload\.classpath
文件 918 2018-12-04 23:14 FileUploadDownload\.project
文件 567 2018-12-04 23:14 FileUploadDownload\.settings\.jsdtscope
文件 364 2018-12-04 23:14 FileUploadDownload\.settings\org.eclipse.jdt.core.prefs
文件 506 2018-12-04 23:14 FileUploadDownload\.settings\org.eclipse.wst.common.component
文件 345 2018-12-04 23:14 FileUploadDownload\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2018-12-04 23:14 FileUploadDownload\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2018-12-04 23:14 FileUploadDownload\.settings\org.eclipse.wst.jsdt.ui.superType.name
文件 4035 2018-12-04 23:16 FileUploadDownload\build\classes\org\gyt\fileUploadDownload\DownloadServlet.class
文件 3026 2018-12-04 23:22 FileUploadDownload\build\classes\org\gyt\fileUploadDownload\ListFileServlet.class
文件 1252 2018-12-04 23:24 FileUploadDownload\build\classes\org\gyt\fileUploadDownload\UploadServlet$1.class
文件 7083 2018-12-04 23:24 FileUploadDownload\build\classes\org\gyt\fileUploadDownload\UploadServlet.class
文件 4352 2018-12-04 22:08 FileUploadDownload\src\org\gyt\fileUploadDownload\DownloadServlet.java
文件 2994 2018-12-04 23:22 FileUploadDownload\src\org\gyt\fileUploadDownload\ListFileServlet.java
文件 7599 2018-12-04 23:24 FileUploadDownload\src\org\gyt\fileUploadDownload\UploadServlet.java
文件 6162 2018-12-04 23:12 FileUploadDownload\WebContent\home.jsp
文件 39 2018-12-04 23:14 FileUploadDownload\WebContent\me
文件 53485 2018-12-04 18:49 FileUploadDownload\WebContent\static\css\animate.min.css
文件 117154 2018-12-04 18:49 FileUploadDownload\WebContent\static\css\bootstrap.min.css
文件 15121 2018-12-04 18:49 FileUploadDownload\WebContent\static\css\st
文件 1097 2018-12-04 18:49 FileUploadDownload\WebContent\static\css\st
文件 10038 2018-12-04 18:49 FileUploadDownload\WebContent\static\css\vegas.min.css
文件 823845 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq1.jpg
文件 940115 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq2.jpg
文件 1447656 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq3.jpg
文件 152207 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq4.jpg
文件 619853 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq5.jpg
文件 228356 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq6.jpg
文件 499486 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\bannerq7.jpg
文件 213 2018-12-04 18:49 FileUploadDownload\WebContent\static\img\overlay.png
............此处省略39个文件信息
相关资源
- commons-lang3-3.4jar.rar
- Android commons ftp使用demo
- commons-logging-1.0.4.jar、json-lib-2.4-jdk15
- java json依赖包 lbc整理,其他有点问题
- json-lib-2.4-jdk15.jar及相关全部依赖包
- commons开发包
- java ResultSet 与 json互转所需要的全部
- org.apache.commons jar包
- 常用的common.jar文件
- Total Commander 8.5 正式官方注册加强版
- apache-common jar包
- org.apache.commons中的各种jar包
- Commons-beanutils jar包
- commons Daemon+Procrun将java程序安装为wi
- Apache commons API(整套全版
- FFmpegCommandHandler的jar包
- FFmpegCommandHandler
- IOUtils(包含apache全部的commons jar包)
- commons-collections.jar
- commons-lang3-3.1包的源代码
- Apache Commons官网下的jar包
- commons-math 3.6.1.jar
- orgapache_commons
- logicaldoc-community-installer-8.3.4.exe
- neo4j-community-3.3.1-windows.zip包含安装简
- commons-io-2.4.jar包 官方免费版
- commons-lang-2.5.jar
- commons-io-2.7.jar
- Java中Httpclient需要的jar包(httpclient.
- org.apache.commons.net.ftp.FTPClient jar包下载
评论
共有 条评论