论坛首页 Java企业应用论坛

struts 上传文件request

浏览 2898 次
精华帖 (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();
		}  
	}

}


论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics