• 大小: 139KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-08
  • 语言: Java
  • 标签:

资源简介

javaweb简单实现文件上传与下载源代码,仅供参考

资源截图

代码片段和文件信息

package com.xpy.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings(“serial“)
public class FileDownLoad extends HttpServlet {

public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {

String fileName=request.getParameter(“fileName“);
//解决参数乱码
  fileName=new String(fileName.getBytes(“iso8859-1“)“UTF-8“);
 
fileName=this.getServletContext().getRealPath(“/WEB-INF/download/“+fileName);
//  System.out.println(fileName+“-------->“+File.separator);
/*
 * 检查文件是否存在、、略
 */
//设置响应头,控制浏览器下载该文件
response.setHeader(“content-disposition“ “attachment;filename=“+URLEncoder.encode(fileName.substring(fileName.lastIndexOf(File.separator)+1)“UTF-8“));

//文件下载
FileInputStream inputStream=new FileInputStream(fileName);
OutputStream outputStream=response.getOutputStream();

byte []buffer=new byte[1024];
int length=0;

while((length=inputStream.read(buffer))!=-1){
outputStream.write(buffer 0 length);
}
inputStream.close();
outputStream.close();
}

public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {

doGet(request response);
}

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-08-25 15:44  FileDownLoad\
     文件        1093  2016-08-25 15:44  FileDownLoad\.classpath
     文件        1337  2016-08-25 16:01  FileDownLoad\.project
     目录           0  2016-08-25 15:44  FileDownLoad\.settings\
     文件         522  2016-08-25 15:44  FileDownLoad\.settings\.jsdtscope
     文件         364  2016-08-25 15:44  FileDownLoad\.settings\org.eclipse.jdt.core.prefs
     文件         495  2016-08-25 15:44  FileDownLoad\.settings\org.eclipse.wst.common.component
     文件         414  2016-08-25 15:44  FileDownLoad\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2016-08-25 15:44  FileDownLoad\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2016-08-25 15:44  FileDownLoad\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2016-08-25 15:45  FileDownLoad\src\
     目录           0  2016-08-25 15:45  FileDownLoad\src\com\
     目录           0  2016-08-25 15:48  FileDownLoad\src\com\xpy\
     目录           0  2016-09-18 18:50  FileDownLoad\src\com\xpy\download\
     文件        1659  2016-09-18 19:56  FileDownLoad\src\com\xpy\download\FileDownLoad.java
     文件        1641  2016-09-18 19:16  FileDownLoad\src\com\xpy\download\ListFileServlet.java
     目录           0  2016-08-25 15:48  FileDownLoad\src\com\xpy\upload\
     文件        8671  2016-09-11 09:16  FileDownLoad\src\com\xpy\upload\FileUpLoadServlet.java
     目录           0  2016-09-18 18:59  FileDownLoad\WebRoot\
     文件        1143  2016-09-18 19:13  FileDownLoad\WebRoot\download.jsp
     文件         834  2016-08-25 15:44  FileDownLoad\WebRoot\index.jsp
     文件         196  2016-08-25 18:46  FileDownLoad\WebRoot\message.jsp
     目录           0  2016-08-25 15:44  FileDownLoad\WebRoot\meta-INF\
     文件          39  2016-08-25 15:44  FileDownLoad\WebRoot\meta-INF\MANIFEST.MF
     文件         889  2016-08-25 15:59  FileDownLoad\WebRoot\upload.jsp
     目录           0  2016-09-18 18:53  FileDownLoad\WebRoot\WEB-INF\
     目录           0  2016-09-14 09:02  FileDownLoad\WebRoot\WEB-INF\classes\
     目录           0  2016-09-14 09:02  FileDownLoad\WebRoot\WEB-INF\classes\com\
     目录           0  2016-09-14 09:02  FileDownLoad\WebRoot\WEB-INF\classes\com\xpy\
     目录           0  2016-09-18 18:50  FileDownLoad\WebRoot\WEB-INF\classes\com\xpy\download\
     文件        2409  2016-09-18 19:56  FileDownLoad\WebRoot\WEB-INF\classes\com\xpy\download\FileDownLoad.class
............此处省略12个文件信息

评论

共有 条评论

相关资源