struts.xml文件配置
<param name="allowTypes"><!--允许上传格式-->
image/bmp,image/jpg,image/png,image/gif,image/jpeg,image/pjpeg
</param>
<param name="savePath">/images/upload</param><!--保存路径-->
private static final long serialVersionUID = 112343242321L;
private String title; private File upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
private String uploadImageName = null; // 接受依赖注入的属性
private String savePath;
// 接受依赖注入的方法
public void setSavePath(String value) { this.savePath = value; }
private String getSavePath() throws Exception { return ServletActionContext.getRequest().getRealPath(savePath); } public void setTitle(String title) { this.title = title; }
public void setUpload(File upload) { this.upload = upload; }
public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; }
public String getTitle() { return (this.title); }
public File getUpload() { return (this.upload); }
public String getUploadContentType() { return (this.uploadContentType); }
public String getUploadFileName() { return (this.uploadFileName); }
@Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
// System.out.println("开始上传单个文件-----------------------");
// System.out.println(getSavePath());
// System.out.println("==========" + getUploadFileName());
// System.out.println("==========" + getUploadContentType());
// System.out.println("==========" + getUpload());
// 判断是否允许上传
String filterResult = filterType(this.getAllowTypes().split(","));
if (filterResult != null) { request.setAttribute("typeError", "您要上传的文件类型不正确!");
request.setAttribute("companyInfo", ucc); return filterResult; }
/** * 验证文件上传的大小 */
String fileLengthResult = FileLength();
if (fileLengthResult != null) { request.setAttribute("typeError", "您要上传的文件超出限制文件的大小!"); request.setAttribute("companyInfo", ucc); return fileLengthResult; }
/** * 上传 */ upload(); /** * 删除原先的图片 */
return SUCCESS; }
/** * 上传图片 * * @throws Exception */
public void upload() throws Exception { uploadImageName = getRandmonFileName();
/* 判断保存文件的路径是否存在 */
File file = new File(getSavePath()); if (file.exists()) { file.mkdir();// 不存在则创建 }
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "//" + uploadImageName); FileInputStream fis = new FileInputStream(getUpload()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } }
/** * 删除用户原先的营业执照图片 * * @param fn * @throws Exception */
public synchronized void deleteImage(String fn) throws Exception { String fileName = getSavePath() + "//" + fn; File file = new File(fileName); System.out.println("file isfile:" + file.isFile()); if (file.isFile()) { file.delete(); } } /
* * 文件名 */
public String getRandmonFileName() { int pos = getUploadFileName().lastIndexOf("."); String type = getUploadFileName().substring(pos); String name = new Date().getTime() + getTitle() + type; return name; }
/** * 验证上文件类型 * * @param types * @return */
public String filterType(String[] types) { String fileType = this.getUploadContentType(); for (String type : types) { if (type.equals(fileType)) { return null; } } return INPUT; }
/** * 验证上传文件大小 * * @param file * @return */
public String FileLength() { if (upload.length() > 512000) { return INPUT; } return null; }
public String getAllowTypes() { return allowTypes; } public void setAllowTypes(String allowTypes) { this.allowTypes = allowTypes; }