Action 类
public ActionForward file(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
FormFile file=loginForm.getMyfile();
/*String type=file.getContentType();
String name=file.getFileName();
int size=file.getFileSize();*/
//将上传的文件放到服务器中
FileOutputStream fos=null;
try {
byte[] data=file.getFileData();
ServletContext application=this.getServlet().getServletContext();
String realPath=application.getRealPath("/Files/");
String fileName=file.getFileName();
System.out.println(fileName);
System.out.println(realPath);
fos=new FileOutputStream(realPath+"/"+fileName);
fos.write(data);
fos.flush();
System.out.println("上传成功至"+realPath+"/"+fileName);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
jsp页面:
<form action="${pageContext.request.contextPath}/login.do?operate=file" method="post" enctype="multipart/form-data" acceptCharset="utf-8">
<input type="file" name="myfile"/ value="浏览"/>
<input type="submit" value="上传"/>
</form>