文件上传首先要引入两个核心包

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

下面是对文件上传和下载的一些代码做的一个简单封装,可以方便以后直接使用【使用时将封装好的jar包直接导入工程中即可使用】

上传文件核心代码

 package com.lizhou.fileload;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.lizhou.exception.FileFormatException;
import com.lizhou.exception.NullFileException;
import com.lizhou.exception.ProtocolException;
import com.lizhou.exception.SizeException; /**
* 上传文件
* @author bojiangzhou
*
*/
public class FileUpload {
/**
* 上传文件用的临时目录
*/
private String tempPath = null;
/**
* 上传文件用的文件目录
*/
private String filePath = null;
/**
* 内存中缓存区的大小,默认100k
*/
private int bufferSize = 100;
/**
* 上传文件的最大大小,默认1M
*/
private int fileSize = 1000;
/**
* 上传文件使用的编码方式,默认UTF-8
*/
private String encoding = "UTF-8";
/**
* 上传文件的格式,默认无格式限制
*/
private List<String> fileFormat = new ArrayList<String>();
/**
* HttpServletRequest
*/
private HttpServletRequest request;
//私有化无参构造
private FileUpload(){} public FileUpload(HttpServletRequest request){
this.request = request;
} public FileUpload(String tempPath, String filePath, HttpServletRequest request){
this.request = request;
this.tempPath = tempPath;
this.filePath = filePath;
makeDirectory(tempPath);
makeDirectory(filePath);
} /**
* 上传文件
* @return 上传成功返回true
* @throws ProtocolException
* @throws FileUploadException
* @throws NullFileException
* @throws SizeException
* @throws FileFormatException
* @throws IOException
*/
public boolean uploadFile()
throws ProtocolException, NullFileException, SizeException, FileFormatException, IOException, FileUploadException{
boolean b = true;
ServletFileUpload upload = getUpload();
//解析request中的字段,每个字段(上传字段和普通字段)都会自动包装到FileItem中
List<FileItem> fileItems = upload.parseRequest(this.request);
for(FileItem item : fileItems){
//如果为普通字段
if(item.isFormField()){
continue;
}
//获取文件名
String fileName = item.getName();
//因为IE6得到的是文件的全路径,所以进一步处理
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
//判断上传的文件是否为空
if(item.getSize() <= 0){
b = false;
throw new NullFileException();
}
//判断文件是否超过限制的大小
if(item.getSize() > fileSize*1000){
b = false;
throw new SizeException();
}
//判断上传文件的格式是否正确
if( !isFormat(fileName.substring(fileName.lastIndexOf(".")+1)) ){
b = false;
throw new FileFormatException();
}
String uuidFileName = getUuidFileName(fileName);
//获取文件的输入流
InputStream is = item.getInputStream();
//创建输出流
OutputStream os = new FileOutputStream(this.filePath+"/"+uuidFileName);
//输出
output(is, os);
//将上传文件时产生的临时文件删除
item.delete();
}
return b;
} /**
* 获取文件上传的输入流
* @return
* @throws ProtocolException
* @throws NullFileException
* @throws SizeException
* @throws FileFormatException
* @throws IOException
* @throws FileUploadException
*/
public InputStream getUploadInputStream()
throws ProtocolException, NullFileException, SizeException, FileFormatException, IOException, FileUploadException{
ServletFileUpload upload = getUpload();
//解析request中的字段,每个字段(上传字段和普通字段)都会自动包装到FileItem中
List<FileItem> fileItems = upload.parseRequest(this.request);
for(FileItem item : fileItems){
//如果为普通字段
if(item.isFormField()){
continue;
}
//获取文件名
String fileName = item.getName();
//因为IE6得到的是文件的全路径,所以进一步处理
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
//判断上传的文件是否为空
if(item.getSize() <= 0){
throw new NullFileException();
}
//判断文件是否超过限制的大小
if(item.getSize() > fileSize*1000){
throw new SizeException();
}
//判断上传文件的格式是否正确
if( !isFormat(fileName.substring(fileName.lastIndexOf(".")+1)) ){
throw new FileFormatException();
}
//获取文件的输入流
InputStream is = item.getInputStream(); return is;
}
return null;
} /**
* 获取上传文件的核心
* @return ServletFileUpload
* @throws ProtocolException
*/
public ServletFileUpload getUpload() throws ProtocolException{
//创建上传文件工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置内存中缓存区的大小
factory.setSizeThreshold(bufferSize);
//如果用户未设置上传文件目录,则设置默认目录
if(filePath == null){
setDefaultFilePath();
}
//如果用户未设置临时存放目录,则设置默认目录
if(tempPath == null){
setDefaultTempPath();
}
//设置上传文件的的临时存放目录
factory.setRepository(new File(this.tempPath));
//创建上传文件对象[核心]
ServletFileUpload upload = new ServletFileUpload(factory);
//设置上传文件的编码方式
upload.setHeaderEncoding(this.encoding);
/*
* 判断客户端上传文件是否使用MIME协议
* 只有当以MIME协议上传文件时,upload才能解析request中的字段
*/
if(!upload.isMultipartContent(this.request)){
throw new ProtocolException();
}
return upload;
} /**
* 输出
* @param is
* @param os
* @throws IOException
*/
public void output(InputStream is, OutputStream os) throws IOException{
byte[] by = new byte[1024];
int len = 0;
while( (len = is.read(by)) > 0 ){
os.write(by, 0, len);
}
is.close();
os.close();
} /**
* 判断上传文件的格式是否正确
* @param format 文件格式
* @return boolean
*/
private boolean isFormat(String format){
if(fileFormat.size() == 0){
return true;
}
for(String f : fileFormat){
if(f.equalsIgnoreCase(format)){
return true;
}
}
return false;
} /**
* 返回文件的UUID名,防止文件名重复
* @param fileName 文件名
* @return uuid名
*/
public String getUuidFileName(String fileName){
return UUID.randomUUID().toString()+"#"+fileName;
} /**
* 设置默认临时目录
*/
private void setDefaultTempPath(){
tempPath = filePath+"/temp"; makeDirectory(tempPath);
} /**
* 设置默认文件目录
* 默认在D盘
*/
private void setDefaultFilePath(){
filePath = "D:/uploadFile"; makeDirectory(filePath);
} /**
* 根据给定的文件目录创建目录
* @param filePath
*/
private void makeDirectory(String filePath){
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
} /**
* 获取临时目录
* @return
*/
public String getTempPath() {
return tempPath;
}
/**
* 设置临时目录
* @param tempPath
*/
public void setTempPath(String tempPath) {
this.tempPath = tempPath;
makeDirectory(tempPath);
}
/**
* 获取文件路径
* @return
*/
public String getFilePath() {
return filePath;
}
/**
* 返回文件路径
* @param filePath
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
makeDirectory(filePath);
}
/**
* 获取内存中缓存区大小
* @return
*/
public int getBufferSize() {
return bufferSize;
}
/**
* 设置内存中缓存区大小
* 默认100k
* @param bufferSize
*/
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
}
/**
* 获取上传文件的最大大小
* @return
*/
public int getFileSize() {
return fileSize;
}
/**
* 限制上传文件的最大大小,单位为k
* 默认1000k
* @param fileSize
*/
public void setFileSize(int fileSize) {
this.fileSize = fileSize;
}
/**
* 返回上传文件的编码方式
* @return
*/
public String getEncoding() {
return encoding;
}
/**
* 设置上传文件的编码方式
* 默认UTF-8格式
* @param encoding
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* 获取允许上传文件的格式
* @return
*/
public String getFileFormat() {
if(fileFormat.size() == 0){
return "*";
}
String format = "";
for(String s:fileFormat){
format += ","+s;
}
format = format.substring(format.indexOf(",")+1);
return format;
}
/**
* 设置上传文件的格式,多个文件格式则多次调用该方法进行设置
* @param fileFormat
*/
public void setFileFormat(String format) {
this.fileFormat.add(format);
} public HttpServletRequest getRequest() {
return request;
} public void setRequest(HttpServletRequest request) {
this.request = request;
} }

下载文件核心代码

 package com.lizhou.fileload;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.lizhou.domain.DownloadFile;
import com.lizhou.exception.FileNotExistsException; /**
* 下载文件
* @author bojiangzhou
*
*/
public class FileDownload {
/**
* 下载时的默认文件目录
*/
private String filePath = null;
/**
* 要下载文件的格式
*/
private List<String> fileFormat = new ArrayList<String>();
/**
* HttpServletRequest
*/
private HttpServletRequest request; public FileDownload(HttpServletRequest request){
this.request = request;
} public FileDownload(String filePath, HttpServletRequest request){
this.request = request;
this.filePath = filePath;
} /**
* 将下载列表绑定到域中
* 默认session
* @param var 域对象变量名
* @throws FileNotExistsException
*/
public void bindDownloadFilesToScope(String var) throws FileNotExistsException{
if(filePath == null){
filePath = "D:/uploadFile";
}
if(!isFileExists(this.filePath)){
throw new FileNotExistsException();
}
List<DownloadFile> list = new ArrayList<DownloadFile>();
getDownloadFiles(filePath, list);
request.getSession().setAttribute(var, list);
} /**
* 获得下载目录下的所有文件
* @param filePath
* @param list
*/
private void getDownloadFiles(String filePath, List<DownloadFile> list){
File file = new File(filePath);
if(file.isFile()){
String uuidFileName = file.getName();
if(isFormat(uuidFileName.substring(uuidFileName.lastIndexOf(".")+1))){
DownloadFile df = new DownloadFile();
df.setFileName(uuidFileName.substring(uuidFileName.indexOf("#")+1));
df.setUuidFileName(uuidFileName);
df.setFilePath(file.getPath());
list.add(df);
}
} else{
File[] childFiles = file.listFiles();
for(File cf : childFiles){
getDownloadFiles(cf.getPath(), list);
}
}
} /**
* 下载文件
* @param var 下载列表的域变量名
* @param uuidFileName 客户端传来的文件的uuid名
* @param response
* @throws IOException
*/
public void downloadFile(String var, String uuidFileName, HttpServletResponse response) throws IOException{
byte[] by = uuidFileName.getBytes("ISO-8859-1");
uuidFileName = new String(by, "UTF-8");
List<DownloadFile> files = (List<DownloadFile>) this.request.getSession().getAttribute(var);
for(DownloadFile file : files){
if(file.getUuidFileName().equals(uuidFileName)){
InputStream is = new FileInputStream(file.getFilePath());
OutputStream os = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(file.getFileName(), "UTF-8"));
output(is, os);
break;
}
}
} public void output(InputStream is, OutputStream os) throws IOException{
byte[] by = new byte[1024];
int len = 0;
while( (len = is.read(by)) > 0 ){
os.write(by, 0, len);
}
is.close();
os.close();
} /**
* 判断文件的格式是否正确
* @param format 文件格式
* @return boolean
*/
private boolean isFormat(String format){
if(fileFormat.size() == 0){
return true;
}
for(String f : fileFormat){
if(f.equalsIgnoreCase(format)){
return true;
}
}
return false;
} /**
* 判断文件目录是否存在
* @param filePath 文件目录
* @return boolean
*/
private boolean isFileExists(String filePath){
boolean b = true;
File file = new File(filePath);
if(!file.exists()){
b = false;
}
return b;
} public String getFilePath() {
return filePath;
} /**
* 设置下载路径
* @param filePath
* @throws FileNotExistsException
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
} /**
* 获取允许上传文件的格式
* @return
*/
public String getFileFormat() {
if(fileFormat.size() == 0){
return "*";
}
String format = "";
for(String s:fileFormat){
format += ","+s;
}
format = format.substring(format.indexOf(",")+1);
return format;
}
/**
* 设置上传文件的格式,多个文件格式则多次调用该方法进行设置
* @param fileFormat
*/
public void setFileFormat(String format) {
this.fileFormat.add(format);
} public HttpServletRequest getRequest() {
return request;
} public void setRequest(HttpServletRequest request) {
this.request = request;
} }

注:上传文件时,前台form表单一定要有如下属性:enctype="multipart/form-data"

附上源码+导出的jar包+使用demo:下载文件

java文件上传下载的更多相关文章

  1. 2013第38周日Java文件上传下载收集思考

    2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...

  2. Java文件上传下载原理

    文件上传下载原理 在TCP/IP中,最早出现的文件上传机制是FTP.它是将文件由客户端发送到服务器的标准机制. 但是在jsp编程中不能使用FTP方法来上传文件,这是由jsp运行机制所决定的 文件上传原 ...

  3. java文件上传下载组件

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

  4. java 文件上传 下载 总结

    首先引入2个jar ![](http://images2017.cnblogs.com/blog/1128666/201711/1128666-20171101145630498-2084371020 ...

  5. java 文件上传下载

    翻新十年前的老项目,文件上传改为调用接口方式,记录一下子~~~ java后台代码: //取配置文件中的上传目录 @Value("${uploadPath}") String pat ...

  6. java文件上传下载解决方案

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...

  7. [Java] 文件上传下载项目(详细注释)

    先上代码,最上方注释是文件名称(运行时要用到) FTServer.java /* FTServer.java */ import java.util.*; import java.io.*; publ ...

  8. java文件上传下载 使用SmartUpload组件实现

    使用SmartUpload组件实现(下载jsmartcom_zh_CN.jar) 2017-11-07 1.在WebRoot创建以下文件夹,css存放样式文件(css文件直接拷贝进去),images存 ...

  9. [java]文件上传下载删除与图片预览

    图片预览 @GetMapping("/image") @ResponseBody public Result image(@RequestParam("imageName ...

随机推荐

  1. Quartz.NET

    http://www.360doc.com/userhome.aspx?userid=11741424&cid=2#

  2. 树的Prufer 编码和最小生成树计数

      Prufer数列 Prufer数列是无根树的一种数列.在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2.它可以通过简单的迭代方 ...

  3. css3创建一个上下线性渐变色背景的div

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  4. 19. 星际争霸之php设计模式--迭代器模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  5. Frag(匹配跟踪)

    ‘碎片’(Frag)跟踪是目标跟踪里的一种通过‘部分‘匹配识别的方法,其目标模板是通过图像多个碎片和块来描述.块是任意的,不基于目标模型的(与传统的基于’部分‘的方法不一样,比如人体的四肢与躯干的跟踪 ...

  6. The import java.util cannot be resolved The import javax.servlet cannot be resolved

    The import java.util cannot be resolved 原因:这是由于你的项目buildpath不对 解决方案:右键项目-------buildpath--------最下面那 ...

  7. Java虚拟机学习(5):类加载器(ClassLoader

    类加载器 类加载器(ClassLoader)用来加载 class字节码到 Java 虚拟机中.一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源文件在经过 Javac之后就被转换成 ...

  8. extjs 一些杂碎的技术问题

    1怎样将grid 的checkedbox 勾选状态都清除 inv.getSelectionModel().clearSelections(); inv.getView().refresh(); 2怎样 ...

  9. Unity中Collider和刚体Collider性能对比

    测试方式: 每个对象做大范围正弦移动,创建1000-5000个对象,保证场景分割树的实时更新,并测试帧率 测试脚本: 移动脚本: using UnityEngine; using System.Col ...

  10. CentOS下LAMP一键yum安装脚本

    本脚本适用环境: 系统支持:CentOS/Redhat/Fedora 内存要求:≥64M 硬盘要求:2GB以上的剩余空间 服务器必须配置好软件源和可连接外网 必须具有系统 root 权限 建议使用干净 ...