Struts1 文件上传 FormFile


<%@ 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;

}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值