浏览 2898 次
锁定老帖子 主题:struts 上传文件request
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-09-23
package com.defined; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.struts.upload.MultipartRequestWrapper; public class MultipartRequest extends MultipartRequestWrapper implements HttpServletRequest { private Map fieldName = null; public MultipartRequest(HttpServletRequest request) throws FileUploadException { super(request); String contentType = request.getContentType(); // 如果是上传文件类型的form if ((contentType != null) && contentType.startsWith("multipart/form-data")) { fieldName = new HashMap(); init(request); } } private void init(HttpServletRequest request) throws FileUploadException { DiskFileUpload diskFileUpload = new DiskFileUpload(); List items = diskFileUpload.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { addTextParameter(item); } else { addFileParameter(item); } } } private void addTextParameter(FileItem item) { String name = item.getFieldName(); String value = null; boolean haveValue = false; String encoding = request.getCharacterEncoding(); if (encoding != null) { try { value = item.getString(encoding); haveValue = true; } catch (Exception e) { } } if (!haveValue) { try { value = item.getString("ISO-8859-1"); } catch (java.io.UnsupportedEncodingException uee) { value = item.getString(); } haveValue = true; } this.setParameter(name, value); } private void addFileParameter(FileItem item) { fieldName.put(item.getFieldName(), item); } public FileItem getFileItem(String name) { return (FileItem) fieldName.get(name); } public String getNotNullParameter(String name) { String temp = this.getParameter(name); return temp == null ? "" : temp; } public Double getDouble(String name) { String temp = this.getParameter(name); return temp == null ? null : Double.valueOf(temp); } public Float getFloat(String name) { String temp = this.getParameter(name); return temp == null ? null : Float.valueOf(temp); } public Integer getInteger(String name) { String temp = this.getParameter(name); return temp == null ? null : Integer.valueOf(temp); } public Date getDate(String name) { String temp = this.getParameter(name); return temp == null ? null : Date.valueOf(name); } public Date getNowDate() { return new java.sql.Date(System.currentTimeMillis()); } //使用方法 public static void main(String[] args) { try { MultipartRequest req = new MultipartRequest(null/*HttpServletRequest request*/); FileItem fileItem = req.getFileItem("fileName"); InputStream in = fileItem.getInputStream(); FileOutputStream fio = new FileOutputStream("d:\\aa.gif"); byte b[] = new byte[1024]; while ((in.read(b, 0, 1024)) != -1) { fio.write(b); fio.flush(); } fio.close(); } catch (FileUploadException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |