ENCTYPE="multipart/form-data"
有JSP页面:
<from name="newsform" method="post" action="create.do" "multipart/form-data">
<input name="text" type="text" size="50">
<input type="file" name="image" size="50">
<input name="Submit" type="submit" value="提交" >
</from>
那么在:Action中怎么获取text的值和要上传的文件呢?
高手指点、谢谢
form中的字段,加上get set方法
private FormFile file;
private String filename;
private String filesize;
action 部分:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String dir="D:/";
UpFileForm uff=(UpFileForm)form;
FormFile file=uff.getFile();
if(file.getFileSize()==0){
return mapping.findForward("success");
}
String fname=file.getFileName();
String size=Integer.toString(file.getFileSize())+"bytes";
InputStream streamIn=file.getInputStream();
OutputStream streamOut=new FileOutputStream(dir+"/"行返则+fname);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=streamIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytesRead);
}
streamOut.close();
streamIn.close();
uff.setFilename(fname);
uff.setFilesize(size);
file.destroy();
return mapping.findForward("success");
}
这样世磨将上传档棚的文件存在d盘
如桥雹乎敏悉果你用的是struts 2 的话
直接在肆卜action中定义两个变量就行了
String text ;
File file ;
再加上 get 和 set 方法
把ActionForm的image定桐虚指局配义誉纳成FormFile类型的在Action 里FormFile file = ActionForm.getImage();