处理提交表单中文本和文件一起上传的解决方案!
发表于: 2005-11-25 上午5:33
回复看到很多人问这类的问题 写一个置顶帖
用apache公司的fileupload可以处理这种情况
项目主页
http://jakarta.apache.org/commons/fileupload/
项目参考
http://jakarta.apache.org/commons/fileupload/using.html
如有不明白的 可以在这下边跟帖
BTW:这个类是处理前边表单中文本和文件一起上传 分别取得表单中文本内容和文件 不要回复和这个不相关的帖子
ayuayufan
发帖: 324
积分: 145
注册: 05-9-23
博客Re:处理提交表单中文本和文件一起上传的解决方案!
发表于: 2005-11-25 下午3:41
原文:
nevercease
回复
板主请看标题:处理提交表单中文本和文件一起上传的解决方案!
我也提出了一种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;
publicclass UploadActionForm extends ActionForm {private FormFile file;
private String name;
public FormFile getFile() {return file;
}publicvoid setFile(FormFile file) {
this.file = file;
}publicvoid setName(String name)
{
this.name=name;
}public String getName()
{return name;
}public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {returnnull;
}publicvoid 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;
publicclass 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 = newbyte[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");
}}
My destiny is forlorn, have to live till it's undone...