package com.action; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.aspectj.util.FileUtil; import com.opensymphony.xwork2.ActionContext; public class UploadAction { private File file; private String fileFileName; // 文件名 private String fileContentType;// 文件类型 private String maximumSize;// 文件大小 private String allowedTypes;// 上传文件容许的类型 public String execute() throws Exception { String realpath = ServletActionContext.getServletContext().getRealPath("/upload"); HttpSession session=ServletActionContext.getRequest().getSession(); HttpServletRequest request=ServletActionContext.getRequest(); boolean falg = false; String fileType[] = allowedTypes.split(","); for (int j = 0; j < fileType.length; j++) { if (fileContentType.equals(fileType[j])) { falg = true; } } if (falg == false) { ActionContext.getContext().put("errormsg", "你上传的格式不是images格式文件!"); return "error_msg"; } if (file.length() > Long.parseLong(maximumSize)) { ActionContext.getContext().put("errormsg", "上传文件超过限制5MB"); return "error_msg"; } fileFileName = fileFileName.toLowerCase();// 获取这个文件转为为小写 /** */ boolean falg1 = false; if (falg1) { ActionContext.getContext().put("errormsg", "该文件已经导入,请选择其他文件!"); return "error_msg"; } /** 正在上传1 */ File uploadFile = new File(realpath); if (!uploadFile.exists()) { uploadFile.mkdir(); } File savefile = new File(new File(realpath), fileFileName); // 创建文件目录 if (!savefile.getParentFile().exists()) { savefile.getParentFile().mkdirs(); } try { FileUtil.copyFile(file, savefile); //session.setAttribute("msgs", "上传成功"); // IOUtils.renameFile(uploadFile, "goods_"); //System.out.println("fileFileName:"+fileFileName); request.setAttribute("msgs",URLEncoder.encode(fileFileName,"UTF-8")); } catch (IOException e) { e.printStackTrace(); } return "success"; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { String[] fileFiles = fileFileName.split("\\."); return UUID.randomUUID().toString()+"."+fileFiles[1]; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getMaximumSize() { return maximumSize; } public void setMaximumSize(String maximumSize) { this.maximumSize = maximumSize; } public String getAllowedTypes() { return allowedTypes; } public void setAllowedTypes(String allowedTypes) { this.allowedTypes = allowedTypes; } }
<action name="upload" class="com.UploadAction"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg, image/pjpeg ,image/bmp, image/gif, image/x-png, </param> <param name="maximumSize">5242880</param> <result name="success">/message.jsp</result> <result name="error_msg">/error.jsp</result> </action>
相关推荐
Struts2上传图片并生成等比例缩略图的实例,在上传文件的同时生成等比例的缩略图,分放在不同的文件夹下。自己亲测,没有问题。
Struts2上传图片 简单的图片上传功能!
使用struts2 上传图片到项目webapps目录下 然后生成缩略图 返回到页面显示缩略图
Jquery+struts2上传图片,制作进度条等示例,还有Jquery+Json+Struts实现Ajax技术,还有图形处理技术等,虽然很少,但都有重点突出,由于我也是学习,所以代码不是很简洁,对想学习交流的朋友非常合适
ckeditor和struts2实现图片上传和编辑器上浏览!
Struts2 上传图片,最初级的入门级例子,适合新手和老鸟
struts2上传图片到Oracle,并进行显示,测试可以成功上传
struts2 上传图片限制.struts2 上传图片限制.struts2 上传图片限制.
NULL 博文链接:https://hacker507.iteye.com/blog/1679273
使用struts2 上传图片,上传图片后生成一张压缩后的图片,和生成一张小图片,在网页上不用显示大图片,显示的缩小后的图片
ssh框架用struts2 hibernate实现图片的上传源码
本案例演示了struts2+Hibernate上传的文件,以blob对象形式保存到数据库中,同时并提供下载样例.
其中包括jsp代码、struts2配置文件代码、上传文件的action代码
实际上,写完多文件上传和下载文章的...1. 如何上传图片,并把图片存入数据库(Mysql)中; 2. 从数据库中读取图片并显示在页面中 注:本文使用的是单纯的Struts2 + Spring + Hibernate,图片操作并没有使用Servlet。
Struts2 的简单上传。功能基本实现。
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2.0图片上传,文件的配置。struts2。0的学习。io的技术。
实现图片的上传和图片的预览,主要用strust2实现的
struts上传视频与照片可以再后页面显示。支持同时上传。