一般将图片长传到服务器上,可能会由于服务器的关闭或重新启动而丢失所有的长传图片,所以备份是很必要的。即在长传图片的时候同时将图片上传一份到服务器外的目录下,这样就可以防止丢失啦!
利用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);//获取存放路径
private String uploadFileName;//文件名称
private String uploadContentType;//文件类型
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();