<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/myAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>
package com.j2ee.zz.action;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.j2ee.zz.actionform.*;
import org.apache.struts.upload.*;
import java.io.*;
public class myAction extends Action {
public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
/**@todo: complete the business logic here, this is just a skeleton.*/
myActionForm myActionForm = (myActionForm) actionForm;
if(actionForm instanceof myActionForm){
String encoding = request.getCharacterEncoding();
if(encoding != null && encoding.equalsIgnoreCase("UTF-8")){
response.setContentType("text/html; charset=gb2312");
}
FormFile file = myActionForm.getTheFile();
try{
InputStream input = file.getInputStream();
String path = request.getRealPath("/");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream out = new FileOutputStream(path+"/"+file.getFileName());
System.out.println(path+"/"+file.getFileName());
int i = 0;
byte[] buff = new byte[8192];
while((i = input.read(buff, 0, 8192)) != -1){
out.write(buff,0,i);
}
out.close();
input.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return actionMapping.findForward("succ");
}
return null;
}
}
package com.j2ee.zz.actionform;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
/**
* <p>Title:UpLoadForm</p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c) 2004 jiahansoft</p>
* <p>Company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/
public class myActionForm extends ActionForm {
public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
protected FormFile theFile;
public FormFile getTheFile() {
return theFile;
}
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = null;
//has the maximum length been exceeded?
Boolean maxLengthExceeded = (Boolean)
request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
{
errors = new ActionErrors();
errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
}
return errors;
}
}