浏览 3465 次
锁定老帖子 主题:commons-fileupload上传
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-23
最后修改:2009-12-23
/** * created FileModel.java by Dirac on 2009-12-3 下午05:50:57 */ package cn.boyi.upload; import java.util.HashMap; import java.util.Map; /** * @author Dirac * @version 1.0 */ public class FileModel { private long size; private int width; private int height; private String path; private String ext; private Map<String, String> params = new HashMap<String, String>(0); /** * @return 返回 path。 */ public String getPath() { return path; } /** * @param path * 要设置的 path。 */ public void setPath(String path) { this.path = path; } /** * @return 返回 height。 */ public int getHeight() { return height; } /** * @param height * 要设置的 height。 */ public void setHeight(int height) { this.height = height; } /** * @return 返回 size。 */ public long getSize() { return size; } /** * @param size * 要设置的 size。 */ public void setSize(long size) { this.size = size; } /** * @return 返回 width。 */ public int getWidth() { return width; } /** * @param width * 要设置的 width。 */ public void setWidth(int width) { this.width = width; } /** * @return 返回 扩展名。 */ public String getExt() { return ext; } /** * @param ext 要设置的 扩展名。 */ public void setExt(String ext) { this.ext = ext; } /** * @return the params */ public Map<String, String> getParams() { return params; } }
/** * created FileDirectoryCreator.java by Dirac on 2009-12-4 上午12:54:07 */ package cn.boyi.upload; /** * @author Dirac * @version 1.0 */ public interface FileDirectoryCreator { /** * 获得文件的生成路径 * @return */ String createFileDirectory(); /** * 获得文件名 * @return */ String createFileName(); /** * 注入参数 * @param name * @param value */ public void put(String name, String value); }
/** * created BaseFileDirectoryCreator.java by Dirac on 2009-12-4 上午01:02:06 */ package cn.boyi.upload; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author Dirac * @version 1.0 */ public class BoyiFileDirectoryCreator implements FileDirectoryCreator { Map<String, String> params = new HashMap<String, String>(0); public BoyiFileDirectoryCreator(){} public BoyiFileDirectoryCreator(String type, Integer id, String fileName){ params.put("type", type); params.put("id", id==null ? null : String.valueOf(id)); params.put("fileName", fileName); } /** * @see cn.boyi.upload.FileDirectoryCreator#createFileDirectory() */ @Override public String createFileDirectory(){ String type = params.get("type"); String id = params.get("id"); Calendar cal = Calendar.getInstance(); StringBuilder fileDirectory = new StringBuilder("/upload/"); if(type!=null && !type.trim().equals(""))fileDirectory.append(type).append("/"); if(id!=null && !id.trim().equals(""))fileDirectory.append(id).append("/"); fileDirectory.append(cal.get(Calendar.YEAR)).append("-"); if(cal.get(Calendar.MONTH)<9)fileDirectory.append("0"); fileDirectory.append((cal.get(Calendar.MONTH) + 1)).append("-"); if(cal.get(Calendar.DAY_OF_MONTH)<10)fileDirectory.append("0"); fileDirectory.append(cal.get(Calendar.DAY_OF_MONTH)).append("/"); return fileDirectory.toString(); } /** * 注入参数 * @param name * @param value */ public void put(String name, String value){ params.put(name, value); } /** * @see cn.boyi.upload.FileDirectoryCreator#createFileName() */ @Override public String createFileName() { String oriParam = getParam("original"); boolean original = oriParam==null ? false : Boolean.valueOf(oriParam); if(original && getParam("oriName")!=null)return getParam("oriName"); String fileName = params.get("fileName"); if(fileName==null)fileName = new SimpleDateFormat("yyyyMMddhhMMss").format(new Date()); return fileName; } /** * 获取参数 * @param name * @return */ private String getParam(String name){ return name==null || name.trim().equals("") ? null : params.get(name); } }
/** * created FileUpload.java by Dirac on 2009-12-3 下午05:52:12 */ package cn.boyi.upload; import java.io.File; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * @author Dirac * @version 1.0 */ public class FileUpload { /** * Uploads file from HttpServletRequest,depends on commons-fileupload-1.2.1 and commons-io-1.3.2 * * @param request * @param basePath * The path of the web context. * @param fileDirectory 负责生成文件上传路径和名称 * @return the uploaded image path which is relative to the web context */ public static FileModel uploadFile(HttpServletRequest request, String basePath, FileDirectoryCreator fileDirectoryCreator) { long maxSize = 1073741824; FileModel model = new FileModel(); if(!ServletFileUpload.isMultipartContent(request))return null; DiskFileItemFactory factory = new DiskFileItemFactory(); //设定内存可以存放文件的最大容量,单位字节,超过的部分会以临时文件的形式存放在硬盘上,这里设置成10兆 factory.setSizeThreshold(10485760); File temp = new File(new StringBuilder(basePath).append("/temp").toString()); if(!temp.exists()){ temp.mkdirs(); } //设置临时文件存放的位置 factory.setRepository(temp); ServletFileUpload upload = new ServletFileUpload(factory); //设置单个文件最大容量限制,-1表示没有限制 upload.setFileSizeMax(-1); //设置整个请求的上传容量限制,-1表示没有限制 upload.setSizeMax(maxSize); String image = ""; try { List<FileItem> items = upload.parseRequest(request); Iterator<FileItem> iterator = items.iterator(); FileItem uploadFile = null; String encoding = request.getCharacterEncoding(); while (iterator.hasNext()) { FileItem item = iterator.next(); org.apache.commons.fileupload.disk.DiskFileItem i; if(item.isFormField()){ if(!item.getFieldName().equals("url")){ model.getParams().put(item.getFieldName(), item.getString(encoding)); fileDirectoryCreator.put(item.getFieldName(), item.getString(encoding)); }else if(item.getFieldName().equals("url")){ String old = item.getString(encoding); if(old!=null && !old.trim().equals("")){ File file = new File(new StringBuffer(basePath).append("/").append(item.getString(encoding)).toString()); if(file.exists())file.delete(); } } continue; } String name = item.getName(); long size = item.getSize(); model.setSize(size); if (size == 0) { continue; } if(name==null || name.equals("")) continue; //System.out.println(name); if(name.indexOf("\\")!=-1){ name = name.substring(name.lastIndexOf("\\") + 1); }else if(name.indexOf("/")!=-1){ name = name.substring(name.lastIndexOf("/")+1); } fileDirectoryCreator.put("oriName", name.substring(0, name.lastIndexOf("."))); model.setExt(name.substring(name.lastIndexOf("."))); uploadFile = item; } Enumeration<String> names = request.getParameterNames(); while(names.hasMoreElements()){ String name = names.nextElement(); String value = request.getParameter(name); if(value!=null && !value.trim().equals(""))fileDirectoryCreator.put(name, value); } String fileDirectory = fileDirectoryCreator.createFileDirectory(); String dir = new StringBuffer(basePath).append(fileDirectory).toString(); String path = new StringBuffer(fileDirectory).append(fileDirectoryCreator.createFileName()).toString(); if (!new File(dir).exists()) { new File(dir).mkdirs(); } String title = model.getParams().get("title"); if(title!=null && !title.trim().equals("")){ path = new StringBuffer(fileDirectory).append(title).toString(); }else{ path = new StringBuffer(fileDirectory).append(fileDirectoryCreator.createFileName()).toString(); } image = new StringBuffer(path).append(model.getExt()).toString(); File file = new File(new StringBuffer(basePath).append(image).toString()); int index = 1; while(file.exists()){ image = new StringBuffer(path).append("(").append(index).append(")").append(model.getExt()).toString(); file = new File(new StringBuffer(basePath).append(image).toString()); ++index; } model.setPath(image); if(uploadFile!=null)uploadFile.write(file); } catch (Exception e) { e.printStackTrace(); } return model; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-12-25
这代码问题很多
|
|
返回顶楼 | |
发表时间:2009-12-26
就发了些代码 , 什么意思?
|
|
返回顶楼 | |