项目图片的备份

一般将图片长传到服务器上,可能会由于服务器的关闭或重新启动而丢失所有的长传图片,所以备份是很必要的。即在长传图片的时候同时将图片上传一份到服务器外的目录下,这样就可以防止丢失啦!

利用struts2对文件经行文件上传:(commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar)
private File upload;//上传文件
private String uploadFileName;//文件名称
private String uploadContentType;//文件类型

String realpath = ServletActionContext.getServletContext().getRealPath("/upload/"+uploadFileName);//获取存放路径
String realpath1 = ServletActionContext.getServletContext().getRealPath("/upload/"+uploadFileName);//获取存放路径(虽然相同,但不能用上面的,不然会失败)
File file = new File(ServletActionContext.getServletContext().getRealPath(""));
//获取tomcat的上级目录存放备份图片
String name = file.getParentFile().getParentFile().getParentFile().toString();
response.setContentType("text/html;charset=UTF-8");
FileInputStream in = new FileInputStream(upload);
FileInputStream in1 = new FileInputStream(upload);
FileOutputStream os = new FileOutputStream(realpath);
FileOutputStream os1 = new FileOutputStream(name+"/upload/"+uploadFileName);
IOUtils.copy(in, os);
IOUtils.copy(in1, os1);
os.flush();
os1.flush();
os.close();
os1.close();
in.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值