private File upload; private String uploadContentType; private String uploadFileName; public void setUpload(File upload) { this.upload = upload; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String add() throws Exception { // 获取客户资质.. 获取页面提交上来的文件。 uploadFileName = UploadUtil.getFileName(uploadFileName); File destFile = new File("H:/photo/img", uploadFileName); //配置 docBase为 docBase="H:/photo/img" path为 path="/crm_xml/img"/> customer.setCust_img("img/"+uploadFileName); FileUtils.copyFile(upload, destFile); upload.delete(); }
下载文件
public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; @SuppressWarnings("deprecation") protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); //先解决响应在客户端的中文乱码问题 response.setContentType("text/html;charset=utf-8"); // 获取客户端传过来的数据 <a href="download?filename=黑马.png"> String filename = request.getParameter("filename"); // 获得文件名并转换成以ISO8859-1编码的字节数组. byte[] bytes = filename.getBytes("ISO8859-1"); // 将字符数组转换成以UTF-8编码的字符串 filename = new String(bytes, "UTF-8"); InputStream inputStream = servletContext.getResourceAsStream("/download/" + filename); // 获得文件名之后,将文件转成输入流 System.out.println("download/" + filename); //给filename来进行编码,对浏览器进行判断,if是火狐就使用Base64进行编码,else就是用UrlEncoder进行编码 String Agent = request.getHeader("User-Agent"); if (Agent.contains("firefox")) { System.out.println("此浏览器是火狐浏览器"); filename = base64EncodeFileName(filename); }else { //否则使用URLEncoder编码 filename = URLEncoder.encode(filename,"UTF-8"); } response.setHeader("Content-Disposition", "attachment;filename="+filename); System.out.println(filename); // 边读边写将数据传给客户端 ServletOutputStream outputStream = response.getOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } inputStream.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * 使用Base64对文件名进行编码 * * @param fileName * @return */ public static String base64EncodeFileName(String fileName) { BASE64Encoder base64Encoder = new BASE64Encoder(); try { return "=?UTF-8?B?" + new String(base64Encoder.encode(fileName.getBytes("UTF-8"))) + "?="; } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new RuntimeException(e); } }
}
需要用到压缩的工具类:
ZipUtils.java
package com.example.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
public static void toZip(String srcDir, OutputStream out, boolean keepDirStructure)
{
long start = System.currentTimeMillis();
ZipOutputStream zos=null;
long end = 0;
try {
zos = new ZipOutputStream(out);
File sourceFile=new File(srcDir);
compress(sourceFile,zos,sourceFile.getName(),keepDirStructure);
end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"ms");
} catch (java.lang.Exception e) {
throw new RuntimeException("to zip from ZipUtils",e);
}finally {
if (zos!=null){
try {
zos.close();
} catch (java.lang.Exception exception) {
exception.printStackTrace();
}
}
}
}
public static void compress(File sourceFile,ZipOutputStream zos,String name,boolean keepDirStructure) throws IOException {
byte[] buf=new byte[1024];
if (sourceFile.isFile()){
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len=in.read(buf))!=1){
zos.write(buf,0,len);
}
zos.close();
in.close();
}else {
File[] listFiles=sourceFile.listFiles();
if (listFiles==null||listFiles.length==0){
if (keepDirStructure){
zos.putNextEntry(new ZipEntry(name+"/"));
zos.closeEntry();
}
}else {
for (File file : listFiles){
if (keepDirStructure){
compress(file,zos,name+"/"+file.getName(),keepDirStructure);
}else {
compress(file,zos,file.getName(),keepDirStructure);
}
}
}
}
public static void toZip(String srcDir, OutputStream out, boolean keepDirStructure)
{
long start = System.currentTimeMillis();
ZipOutputStream zos=null;
long end = 0;
try {
zos = new ZipOutputStream(out);
File sourceFile=new File(srcDir);
compress(sourceFile,zos,sourceFile.getName(),keepDirStructure);
end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"ms");
} catch (java.lang.Exception e) {
throw new RuntimeException("to zip from ZipUtils",e);
}finally {
if (zos!=null){
try {
zos.close();
} catch (java.lang.Exception exception) {
exception.printStackTrace();
}
}
}
}
public static void compress(File sourceFile,ZipOutputStream zos,String name,boolean keepDirStructure) throws IOException {
byte[] buf=new byte[1024];
if (sourceFile.isFile()){
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len=in.read(buf))!=1){
zos.write(buf,0,len);
}
zos.close();
in.close();
}else {
File[] listFiles=sourceFile.listFiles();
if (listFiles==null||listFiles.length==0){
if (keepDirStructure){
zos.putNextEntry(new ZipEntry(name+"/"));
zos.closeEntry();
}
}else {
for (File file : listFiles){
if (keepDirStructure){
compress(file,zos,name+"/"+file.getName(),keepDirStructure);
}else {
compress(file,zos,file.getName(),keepDirStructure);
}
}
}
}
}
}
}