首页 > 代码库 > java上传附件,批量下载附件(一)
java上传附件,批量下载附件(一)
上传附件代码:借助commons-fileupload-1.2.jar
package com.str;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
public class UploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
/* response.setContentType("text/html;charset=gb2312");
response.setCharacterEncoding("utf-8");*/
OutputStream outputStream = null;
InputStream inputStream = null;
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
try {
List items = fileUpload.parseRequest(request);
for (Iterator iterator = items.iterator(); iterator.hasNext();) {
FileItem name = (FileItem) iterator.next();
if(!name.isFormField()){
String fieldName = name.getFieldName(); //这个是name值
String fileName = name.getName(); //这个是全路径
String lastFileName ="";
//这句话获取的是源文件的原名称,不做任何修改
String oldNamePath = fileName.substring(fileName.lastIndexOf("\\")+1);
if(fileName.endsWith(".docx")|| fileName.endsWith(".xls")){
lastFileName = request.getRealPath("/")+"\\upload\\"+ oldNamePath;
outputStream = new FileOutputStream(new File(lastFileName ));
inputStream = name.getInputStream();
byte[] bs = new byte[1024];
int length = 0;
while(null != inputStream && (length = inputStream.read(bs))!=-1){
outputStream.write(bs);
}
}
outputStream.flush();
}
//把lastFileName存到数据库(这里就不写了不只lz用的什么方式)}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
单个下载文件,批量下载文件代码:借助于ant.jar包的ZipOutputStream、ZipEntry
package com.str;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class LoadServlet extends HttpServlet {
@Override
/*protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String path = getServletContext().getRealPath("/") + "\\upload";
OutputStream o = resp.getOutputStream();
byte b[] = new byte[1024];
//这个地方的文件,可以从数据库中动态查找,我这边写死了为了简单展示
File fileLoad = new File(path, "解析类型配置.xls");
String filename = new String("解析类型配置.xls".getBytes("gbk"), "iso8859-1");
System.out.println(filename);
我记得在Excel导出数据的时候说过filename千万别写中文,其实经过以下:
String filename = new String("解析类型配置.xls".getBytes("gbk"), "iso8859-1");
转换以后,文件无论是中文、英文,都不会出现乱码情况,本人已验证
resp.setHeader("Content-disposition", "attachment;filename="+ filename);
long fileLength = fileLoad.length();
String length = String.valueOf(fileLength);
resp.setHeader("Content_Length", length);
FileInputStream in = new FileInputStream(fileLoad);
int n = 0;
while ((n = in.read(b)) != -1) {
o.write(b, 0, n);
}
}*/
//以上是单个下载附件,这边是批量压缩下载附件
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String zipFileName = "test.zip";
//这些文件都是存在的,我是写死了的,可以从页面传名称过来
String[] filePathArray = {"1.jpg","2.jpg","3.xls","测试.docx"};
String path = getServletContext().getRealPath("/") + "\\image";
resp.setContentType("application/x-msdownload" ); // 通知客户文件的MIME类型:
resp.setHeader("Content-disposition","attachment;filename=" + zipFileName);
ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
for (String filePath : filePathArray) {
File file = new File(path + File.separator + filePath);
doZip(file, zos);
}
zos.close();
}
//处理批量下载时候,文件压缩问题
private void doZip(File file, ZipOutputStream zos) throws IOException {
if(file.exists()) {
if (file.isFile()) {
//如果是文件,写入到 zip 流中
String fileName = file.getName();
ZipEntry zet = new ZipEntry(file.getName());
zos.putNextEntry(zet);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
zos.setEncoding("gbk"); //这个地方很重要
zos.flush();
fis.close();
}else {
System.out.println("不是文件,那就不下载了,因为前台会做处理,此处就不在一步步进行验证了!");
}
}
}
}
java上传附件,批量下载附件(一)