一个简单的struts实现文件上传示例

转载:
http://hi.baidu.com/javajavajava/blog
upload.jsp

<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html> 
<head>
   <title>JSP for UploadForm form</title>
</head>
<body>
   <html:form action="/upload" enctype="multipart/form-data">
    file : <html:file property="file"/><html:errors property="file"/><br/>
    <html:submit value="上传"/>
   </html:form>
   <hr>
   <logic:present name="fileName">
   <html:link href="<%=basePath+"myupload/"+request.getAttribute("fileName") %>"
   target="_blank" useLocalEncoding="gb2312">
    <bean:write name="fileName"/>
   </html:link>
   <bean:write name="size"/>
   </logic:present>
</body>
</html>

struts-config.xml

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

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

<global-exceptions />
<global-forwards />
<action-mappings >
    <action
      attribute="uploadForm"
      input="/upload.jsp"
      name="uploadForm"
      path="/upload"
      scope="request"
      type="com.huqianhao.struts.action.UploadAction">
      <forward name="upload" path="/upload.jsp" />
    </action>

</action-mappings>

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

UploadAction.java

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

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.huqianhao.struts.form.UploadForm;

/**
* MyEclipse Struts Creation date: 01-12-2008
* 
* XDoclet definition:
* 
* @struts.action path="/upload" name="uploadForm" input="/upload.jsp"
*                scope="request" validate="true"
*/
public class UploadAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* 
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
   UploadForm uploadForm = (UploadForm) form;
   FormFile file = uploadForm.getFile();
   String fileName = file.getFileName();
   String contentType = file.getContentType();
   String size = (file.getFileSize() + " bytes");
   try {
    InputStream stream = file.getInputStream();
    // 把内容写到指定的文件中
    OutputStream bos = new FileOutputStream(request.getRealPath("")
      + "/myupload/" + fileName);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
     bos.write(buffer, 0, bytesRead);
    }
    bos.close();
    stream.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
   request.setAttribute("fileName", fileName);
   request.setAttribute("size", size);
   request.setAttribute("contentType", contentType);
   return mapping.findForward("upload");
}
}

UploadForm.java

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.huqianhao.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: 01-12-2008
* 
* XDoclet definition:
* @struts.form name="uploadForm"
*/
public class UploadForm extends ActionForm {
/*
* Generated fields
*/

/** file property */
private FormFile file;

/*
* 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
}

public FormFile getFile() {
   return file;
}

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

你可能感兴趣的:(java,apache,jsp,struts,MyEclipse)