近来在javaeye论坛发的时间比较多!这是一个对于java爱好者很好的论坛!加油.........
每天努力一分
每天努力一分
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub
FormFile myfile = uploadForm.getMyfile();
/*得到文件的基本信息
String type = myfile.getContentType(); //得到文件类型
String name = myfile.getFileName(); //得到文件名称,如果不选而提交,名称为空字符串
int size = myfile.getFileSize(); //得到文件大小
System.out.println(type);
System.out.println(name);
System.out.println(size);*/
//保存文件在服务器端
FileOutputStream fos = null;
try{
byte[] data = myfile.getFileData();
String fileName = myfile.getFileName();
//Java如果要访问服务器硬盘,一定要提供逻辑路径(硬盘上的路径c:\..),应用默认在tomcat的bin目录下
//怎样将URL相对路径/FILES/转成硬盘上的绝对路径?用application
ServletContext application = this.getServlet().getServletContext();
String realPath = application.getRealPath("/FILES/");
fos = new FileOutputStream(realPath + "/" + fileName);
fos.write(data);
}
catch(Exception ex){ex.printStackTrace();}
finally{
try{
fos.close();
}catch(Exception ex){}
}
/*可以得到文件的输入流
InputStream is = myfile.getInputStream();//得到文件的输入流,可以对文件进行分析
BufferedReader br = new BufferedReader(new InputStreamReader(is));//通过BufferedReader读文件
*/
return new ActionForward("/upload.jsp");
}
}