|
发表于 2011-10-31 09:50:50
|
显示全部楼层
Re:$处理提交表单中文本和文件一起上传的解决方案
板主请看标题:处理提交表单中文本和文件一起上传的解决方案!
我也提出了一种struts解決方案,为何不可?同样是文本和文件一起上传,分别取得表单中文本内容和文件,为何要删?我无意顶撞斑竹,只是写写我的看法而已嘛~
JSP:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>
uploadfile
</title>
</head>
<body bgcolor="#ffffff">
<html:form action="uploadAction.do" enctype="multipart/form-data">
<html:file property="file">
</html:file>
<html:text property="name">
</html:text>
<html:submit>
</html:submit>
</html:form>
</body>
</html>
actionForm:
package uploadtst;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class UploadActionForm extends ActionForm {
private FormFile file;
private String name;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
Action:
package uploadtst;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;
import java.io.*;
import javax.servlet.http.HttpSession;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
UploadActionForm uploadActionForm = (UploadActionForm) actionForm;
FormFile file=uploadActionForm.getFile();
String name=uploadActionForm.getName();
HttpSession session=servletRequest.getSession();
session.setAttribute("name",name);
try{
InputStream is=file.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
OutputStream os=new FileOutputStream("f:/a.jpg");
BufferedOutputStream bos=new BufferedOutputStream(os);
byte[] temp = new byte[1024];
int length=0;
while((length=bis.read(temp))!=-1)
{
bos.write(temp,0,length);
}
bos.close();
bis.close();
file.destroy();
}catch(Exception e)
{
e.printStackTrace();
}
return actionMapping.findForward("forward");
}
} |
|