欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

单文件上传 博客分类: struts StrutsMyeclipseApacheJavaOS 

程序员文章站 2024-03-16 09:41:10
...
上传的表单method应设为post,enctype应设为multipart/form-data


--------------------------示例---------------------------------------
upload.jsp
=====================
<form action="upload.do?method=upload" method="post"
		enctype="multipart/form-data">
		select the file you wish to upload:
		<input type="file" name="file" />
		<br />
		<input type="text" name="name"/>
		<input type="submit" value="submit" />
		<input type="reset" value="cancel" />
	</form>
	<%
			if (request.getAttribute("result") != null) {
			out.println(request.getAttribute("result").toString());
		}
	%>


UploadForm.java
=====================
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.supersit.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/** 
 * MyEclipse Struts
 * Creation date: 10-23-2007
 * 
 * XDoclet definition:
 * @struts.form name="uploadForm"
 */
public class UploadForm extends ActionForm {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method validate
	 * @param mapping
	 * @param request
	 * @return ActionErrors
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 
	 * Method reset
	 * @param mapping
	 * @param request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
	}
	
	private FormFile file;
	
	private String name;
	

	public FormFile getFile() {
		return file;
	}

	public void setFile(FormFile file) {
		this.file = file;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}


UploadAction.java
==========================
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.supersit.struts.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.supersit.struts.form.UploadForm;
import com.supersit.struts.util.DateTime;
import com.supersit.struts.util.MD5;

/**
 * MyEclipse Struts Creation date: 10-23-2007
 * 
 * XDoclet definition:
 * 
 * @struts.action input="/upload.jsp"
 */
public class UploadAction extends DispatchAction {
	/*
	 * Generated Methods
	 */

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		UploadForm uploadform = (UploadForm) form;

		String context = this.servlet.getServletContext().getRealPath("/");
		
		System.out.println("context="+context);

		org.apache.struts.upload.FormFile formfile = uploadform.getFile();
		java.io.InputStream is = null;
		java.io.OutputStream os = null;
		try {
			is = formfile.getInputStream();

			String ext=formfile.getFileName().split("\\.")[1];
			String filename = new MD5().getMD5ofStr(formfile.getFileName()
					+ new DateTime().getCurrentDate5())
					+ "." + ext;

			System.out.println("filename===" + filename);

			os = new FileOutputStream(context + "/photo/" + filename);

			byte[] bytes = new byte[8192];
			while ((is.read(bytes, 0, 8192)) != -1) {
				os.write(bytes, 0, 8192);
			}

			if (os != null)
				os.close();
			if (is != null)
				is.close();
			
			request.setAttribute("result", "上传成功");
			
			
			
			String name=uploadform.getName();
			
			name=new String(name.getBytes("ISO8859-1"),"utf-8");
			
			System.out.println("name==="+name);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			request.setAttribute("result", "上传失败");
		} catch (IOException e) {
			e.printStackTrace();
			request.setAttribute("result", "上传失败");
		}

		return mapping.findForward("upload");
	}
}



struts-config.xml
=================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="uploadForm" type="com.supersit.struts.form.UploadForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      name="uploadForm"
      attribute="uploadForm"
      input="/upload.jsp"
      path="/upload"
      type="com.supersit.struts.action.UploadAction"
      validate="false" 
      parameter="method">
      <forward name="upload" path="/upload.jsp"></forward>
            </action>

  </action-mappings>

  <message-resources parameter="com.supersit.struts.ApplicationResources" />
</struts-config>