• 大小: 7.12MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-08-22
  • 语言: Java
  • 标签: Comm  

资源简介

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.xml

     文件         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\meta-INF\MANIFEST.MF

     文件      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\style.css

     文件       1097  2018-12-04 18:49  FileUploadDownload\WebContent\static\css\style2.css

     文件      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个文件信息

评论

共有 条评论