struts 文件上传

近来在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");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值