我的上传组件-开始干活啦----3上传处理的action和其它方法
2009-11-10 04:41
330 查看
upload.action
public class UploadAction extends BaseAction { private static final long serialVersionUID = -7472787698325813464L; private File[] upload; // struts拦截到的上传文件类型,在表单名后加ContentType private String[] uploadContentType; // 上传的文件名,在表单名后加FileName private String[] uploadFileName; // 上传保存的路径 private String savePath; private String[] targetFileName; /************************ method ******************************/ @Override public String execute() throws Exception { setTargetFileName(UploadUtil.getUploadPath(upload, savePath, uploadFileName)); return SUCCESS; } /*************************get set ****************************/ }
UploadUtil
package com.flexoa.util.file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import org.apache.struts2.ServletActionContext; /** * 写文件(IO操作) 到目标位置 重新命名文件 * * @author Administrator * */ public class UploadUtil { private static final int BUFFER_SIZE = 16 * 1024; /** * 上传的处理方法,这里还调用了上传的类 返回上传完后,拼装好的路径,数据库只保存路径 * * @return photo */ public static String[] getUploadPath(File[] upload, String savePath, String[] photosFileName) { String destPath = ServletActionContext.getServletContext().getRealPath( savePath); // System.out.println(" destPath : " + destPath); // 先检查目录存在否 File filePath = new File(destPath); // 如果不存在则创建 if (!filePath.exists()) { filePath.mkdirs(); } /** * 先定义转义字符,如果上传的file表单有值,就开始重新命名(有单独的方法),写文件(有单独的方法)到目标位置 */ String[] targetPath = new String[upload.length]; if (upload != null) { // 文件保存的路径,并初始长度 String[] myDir = new String[upload.length]; // 文件最终的名字 String[] tName = new String[upload.length]; // 取目标目录 // System.out.println(" savePath : " + savePath); for (int i = 0; i < upload.length; i++) { // 生成文件名 tName[i] = UploadUtil.generateFileName(photosFileName[i]); targetPath[i] = savePath + "/" + tName[i]; // 拼装保存的路径 myDir[i] = destPath + "//" + tName[i]; // 建立目标文件,将文件复制到目录 File destFile = new File(destPath, tName[i]); UploadUtil.writeFile(upload[i], destFile); } } return targetPath; } /** * 写文件 * @param src * 传递增来的源文件 * @param goal * 写入的最终文件 */ public static void writeFile(File src, File goal) { InputStream is = null; OutputStream os = null; try { is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); os = new BufferedOutputStream(new FileOutputStream(goal), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); } catch (IOException e) { throw new RuntimeException("文件上传异常!"); } } /** * 构造文件名 */ public static String generateFileName(String fileName) { // 按格式取时间 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss_"); String strDate = dateFormat.format(new Date()); // 在10000里抽奖,生成编号 int random = new Random().nextInt(100000); // 设置获得文件名后缀的字符 int position = fileName.lastIndexOf("."); String extension = fileName.substring(position); // 拼装并返回 return strDate + random + extension; } }
struts-upload.xml
<struts> <constant name="struts.custom.i18n.resources" value="message"></constant> <!-- 上传文件最大值 --> <constant name="struts.multipart.maxSize" value="10240000" /> <package name="uploadGroup" extends="struts-default" namespace="/"> <action name="upload" class="uploadBean"> <result>/include/upload/upload_success.jsp </result> <result name="input">/include/upload/upload_success.jsp </result> <interceptor-ref name="fileUpload"> <!-- 配置允许上传的文件类型,多个用","分隔 --> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg ,image/x-png, image/pjpeg </param> <!-- 配置允许上传的文件大小,单位字节 --> <param name="maximumSize">10240000</param> </interceptor-ref> <!-- 动态设置Action中的savePath属性的值 --> <param name="savePath">${savePath}</param> <interceptor-ref name="defaultStack" /> </action> </package> </struts>
相关文章推荐
- 我的上传组件-开始干活啦----1
- 我的上传组件-开始干活啦----2表单页面upload.jsp
- 我的上传组件-开始干活啦----4上传成功的页面,提供预览,删除功能
- php实现表单多按钮提交action的处理方法
- 通过form的action属性提交表单并接收处理返回值的方法(页面不跳转)
- php实现表单多按钮提交action的处理方法
- 日志处理(二) 日志组件logback的介绍及配置使用方法(转)
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射 (
- Asp.NET大文件上传组件开发总结(三)---处理文件内容
- 前后端分离之图片上传服务端处理方法
- 7. spring MVC 拦截器组件,异常处理,文件上传
- 关于struts如何在action中实现多个处理方法
- action接收到来自jsp页面的请求时出现中文乱码问题处理方法
- 注册界面的优化之ActionBar组件的应用之(二)ActionBar组件的事件处理
- 自动化处理上传下载对话框的方法总结
- thinkphp 多图和单图上传处理 (我认为这个方法更好吧 比我以前写的 高效了不少)
- 使用Commons-fileupload 组件上传时,获取表单其它元素系列问题集锦
- Struts2之Action处理多个方法总结
- Spring MVC 学习总结(三)——请求处理方法Action详解
- flash上传组件swfupload的坑点及解决方法