struts1 上传处理


<form name="form" id="form" action="/zyk/zykpic.do" method="post" enctype="multipart/form-data" >
<input type="file" name="zykpicfile" id="zykpicfile" />支持格式:.jpg.jepg.bmp.png.gif
</form>



private FormFile zykpicfile;

public FormFile getZykpicfile() {
return zykpicfile;
}

public void setZykpicfile(FormFile zykpicfile) {
this.zykpicfile = zykpicfile;
}





public List picSet(FormFile picfile,File dir,String ymdhms,String file_configpath){
ArrayList l_picSet=new ArrayList();
FileOutputStream out = null;
try {
byte[] b = picfile.getFileData();// 取得文件数据
String fileName=ymdhms;//文件名称前缀
String extName = picfile.getFileName().substring(picfile.getFileName().lastIndexOf(".")); // 得到上传文件的扩展名
//.jpg.jepg.bmp.png.gif
if(extName.equals(".jpg") || extName.equals(".jepg") ||extName.equals(".bmp") ||extName.equals(".png") ||extName.equals(".gif")){
l_picSet.add("file");
}else{
l_picSet.add("errorfile");
}
l_picSet.add(picfile.getFileSize()/1024);//把图片大小转换为kb
String upname=fileName+extName;
l_picSet.add(upname);//把图片名称
String file_path=dir + File.separator + upname;
File outfile = new File(file_path);
out = new FileOutputStream(outfile, false);
out.write(b); // 通过流将数据写入文件
//处理图片大小
ImageSize imageSize=new ImageSize();
imageSize.zoomImg(file_path,file_configpath, 120, 80,"small"+upname);
}catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null) {
try {
out.close(); // 关闭文件输出流
} catch (IOException e) {
e.printStackTrace();
}
}
}

return l_picSet;
}





//D:\tomcat6\webapps\zyk/pic/2011/06/15/20110615083615.jpg
String path_URL=sys_path+pic_configpath+datepath+"/"+filename;
File f = new File(path_URL);
if(f.exists()){
//需要判断是否存在
}

//-------------------------------


try {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;

//如果是从服务器上取就用这个获得系统的绝对路径方法。 String filepath = servlet.getServletContext().getRealPath("/" + path);
String filepath=path_URL;
File uploadFile = new File(filepath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
//这个就就是弹出下载对话框的关键代码
response.setHeader("Content-disposition","attachment;filename=" +filename);
int bytesRead = 0;
//用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流
byte[] buffer = new byte[65535];
while ((bytesRead = bis.read(buffer, 0, 65535)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



//访问那个action即可
<a href="/zyk/zykpic.do?method=downfile&picid=001">下载</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值