java文件上传下载
文件上传首先要引入两个核心包
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文件上传下载的更多相关文章
- 2013第38周日Java文件上传下载收集思考
2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
- Java文件上传下载原理
文件上传下载原理 在TCP/IP中,最早出现的文件上传机制是FTP.它是将文件由客户端发送到服务器的标准机制. 但是在jsp编程中不能使用FTP方法来上传文件,这是由jsp运行机制所决定的 文件上传原 ...
- java文件上传下载组件
需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...
- java 文件上传 下载 总结
首先引入2个jar  String pat ...
- java文件上传下载解决方案
javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...
- [Java] 文件上传下载项目(详细注释)
先上代码,最上方注释是文件名称(运行时要用到) FTServer.java /* FTServer.java */ import java.util.*; import java.io.*; publ ...
- java文件上传下载 使用SmartUpload组件实现
使用SmartUpload组件实现(下载jsmartcom_zh_CN.jar) 2017-11-07 1.在WebRoot创建以下文件夹,css存放样式文件(css文件直接拷贝进去),images存 ...
- [java]文件上传下载删除与图片预览
图片预览 @GetMapping("/image") @ResponseBody public Result image(@RequestParam("imageName ...
随机推荐
- Java中String类的方法及说明
String : 字符串类型 一. String sc_sub = new String(c,3,2); // String sb_copy = new String(sb) ...
- SQL数据库操作命令大全
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- vsftpd.conf 怎么保存
引用:http://zhidao.baidu.com/question/169939742.html vsftpd.conf 用vim编辑后如何保存?网上很多我都看了 esc 然后shift+ 不好使 ...
- 学习jQuery之旅
早就听说了Jquery的大名,一直没有细心的学习一下,通过阅读收集的一些资料,感觉Jquery真的很强大.决定开始自己的学习Jquery之旅.在这里不是为大家讲解Jquery(深知水平有限),只是将自 ...
- C#计算器代码
在刚刚接触c#的时候,就想做一个简单加减乘除计算器.这就是目标,可惜一直没有动手去做,今天特意把它简单做了.很简单,很简单,了却一个心愿了. 代码: using System; using Syste ...
- OBD K线抓包 III
14230 HL激活, 5BPS又称 00 //电平激活 C1 33 F1 81 66 //14230的Enter命令 83 F1 11 C1 EF 8F C4 //回应了,一个命令就回应了... ...
- 为您详细比较三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...
- 为PetaPoco添加实体模板
Brad为我们提供了T4模板,因为公司一直在使用CodeSmith,故为其写了一个CodeSmith的模板,代码如下: <%-- Name:EntityTemplates Author: Des ...
- src url href uri的区别和联系
- 判断ie?
<!DOCTYPE html> <html lang="en"> <head> <script type="text/javas ...