jsp代碼:
<html:form action="HtmlFile.do" method="post" enctype="multipart/form-data">
select file:
<html:file property="file"/>
<html:submit/>
</html:form>
Form代碼:
private FormFile file;
public FormFile getFile(){return this.file;}
public void setFile(FormFile file){this.file=file;}
Action代碼:
String dir=servlet.getServletContext().getRealPath("/upload");
HtmlFileForm hff = (HtmlFileForm)form;
FormFile file = hff.getFile();
if(file==null){
return mapping.findForward("success");
}
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize()+"bytes");
InputStream in = file.getInputStream();
OutputStream out = new FileOutputStream(dir+"/"+fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while(bytesRead = in.read(buffer,0,8192)!=-1){
out.write(buffer,0,bytesRead);
}
out.close();
in.close();
聯(lián)系客服