使用Struts进行文件上传
使用Struts进行文件上传需要Struts提供的类:org.apche.struts.uploadFormfile 改类在lib目录下的commons-fileuoload.jar文件中:
上传表单代码:
<html:form action="/upload" enctype="multipart/form-data">
<html:file property="file">
<input type="submit" value="上传" name="submit">
<input type="reset" value="重设">
对应的ActionForm代码:
public class FileActionForm extends ActionForm{
private Formfile file;
public void setFile(Formfile file){
this.file = file;
}
public Formfile getFile(){
return file;
}
}
控制文件上传的Action类:
public class uploadAction extends Action{
public ActionForWard execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){
FileActionForm fileform = (FileActionForm)form;
if(fileform != null){
Formfile file =(Formfile)fileform.getFile();
if(file != null){
String filename = file.getFileName();
try{
byte[] files = file.getFileData();
//uploadfile为上传的存储路径,以原文件的名称为上传文件的名称
FileOutputStream out =
new FileOutputStream(servlet.getServletContext()
.getRealPath(""/)+
"uploadfile" + filename);
System.out.println("文件开始上传.....");
out.write(files);
System.out.println("文件上传完毕......");
out.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
return mapping.findForword("....");
}
}
补充:Jsp教程,Java基础