webwork 多图片上传

xiaoxiao2022-06-12  33

近期用webwork做项目,有上传文件操作,在此记录一下,有部分内容来自网络,javascript脚本只支持IE,不支持firefox。 一、FileUploadAction.java 文件; package ebizserve.cpd.view.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.log4j.Logger; import ebizserve.util.FileUtil; public class FileUploadAction extends BaseAction { private Logger logger = Logger.getLogger(FileUploadAction.class); private static final long serialVersionUID = 5943699833329252614L; // 对应<input type=file name=fileImages />中的file控件name名称; private File[] fileImages; // 获取 文件名称为 fileImages 的 file 控件上传文件的文件名称; private String[] fileImagesFileName; // 默认调用方法 public String execute() { File[] newFileImages = FileUtil.renameToFiles(fileImagesFileName, fileImages); if (newFileImages != null && newFileImages.length > 0) { for (File file : newFileImages) { try { if (!FileUtil.verifyFile(file, new String[] { "jpg", "gif", "jpeg", "png" })) { this.addFieldError("errorimage", "Error:Invalid File Type"); return "create"; } } catch (Exception e) { e.printStackTrace(); } } for (File fileImage : newFileImages) { try { FileInputStream fileIn; int filesize = 0; String imageName = fileImage.getName(); fileIn = new FileInputStream(fileImage); filesize = fileIn.available(); logger.info("获得文件名称: " + imageName); logger.info("获得文件大小:" + filesize); FileUtil.saveFile(fileImage, imageName, getProductFilePath()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } return SUCCESS; } // 对应的 getXXX()/setXXX()方法; public File[] getFileImages() { return fileImages; } public void setFileImages(File[] fileImages) { this.fileImages = fileImages; } public String[] getFileImagesFileName() { return fileImagesFileName; } public void setFileImagesFileName(String[] fileImagesFileName) { this.fileImagesFileName = fileImagesFileName; } } 二、BaseAction.java 文件; package ebizserve.cpd.view.action; import java.io.File; import com.opensymphony.xwork.ActionSupport; public class BaseAction extends ActionSupport { private static final long serialVersionUID = -2796680436059696842L; private static final String FILE_PATH = "d:/upload/"; // 文件realpath; public String getProductFilePath() { File filesPath = new File(FILE_PATH); if (!filesPath.exists()) { // (canRead() filesPath.mkdirs(); // mkdirs() } return FILE_PATH; } } 三、FileUtil.java 文件; package ebizserve.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileUtil { // 重命名文件; public static File[] renameToFiles(String[] fileNames, File[] files) { File[] retFiles = null; if (fileNames != null && fileNames.length > 0) { retFiles = new File[fileNames.length]; for (int i = 0, n = fileNames.length; i < n; i++) { File dist = new File(fileNames[i]); files[i].renameTo(dist); retFiles[i] = dist; } } return retFiles; } // save文件; public static long saveFile(File file, String fileName, String filePath) throws Exception { if (file == null) { return 0; } File filepath = new File(filePath); if (!filepath.isDirectory()) filepath.mkdirs(); File filedesc = new File(filepath, fileName); return copyFile(file, filedesc); } // copy文件; public static long copyFile(File fromFile, File toFile) { long len = 0; InputStream in = null; OutputStream out = null; try { in = new FileInputStream(fromFile); out = new FileOutputStream(toFile); byte[] t = new byte[1024]; int ii = 0; while ((ii = in.read(t)) > 0) { out.write(t, 0, ii); len += ii; } } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (Exception e) { e.printStackTrace(); } } } return len; } // 验证文件正确性; public static boolean verifyFile(File file, String[] exts) throws Exception { boolean flag = false; if (file != null) { String ext = getExtension(file.getName()); if (ext == null) { return false; } if (exts != null && exts.length > 0) { if (exts[0].equals("*.*")) return true; for (int i = 0; i < exts.length; i++) { if (ext.equalsIgnoreCase(exts[i])) { flag = true; break; } } } } return flag; } // 取得文件扩展名; public static String getExtension(String fileName) { int newEnd = fileName.length(); int i = fileName.lastIndexOf('.', newEnd); if (i != -1) { return fileName.substring(i + 1, newEnd); } else { return null; } } } 四、Web.xml配置文件; <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>webwork</filter-name> <filter-class> com.opensymphony.webwork.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>webwork</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app> 五、xwork.xml 配置文件; <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"> <xwork> <include file="webwork-default.xml" /> <package name="default" extends="webwork-default"> <interceptors> <interceptor-stack name="validationStack"> <interceptor-ref name="validation" /> <interceptor-ref name="component" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="validationStack" /> </package> <!-- 引用多个 xwork_*.xml --> <include file="action/xwork_upload.xml" /> </xwork> 六、xwork_upload.xml 配置文件; <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"> <xwork> <package name="productbase" extends="default"> <interceptors> <interceptor name="upload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"> <param name="maximumSize">10000000</param> </interceptor> <interceptor-stack name="uploadStack"> <interceptor-ref name="upload" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="WSStack" /> <action name="doUpload" class="ebizserve.cpd.view.action.FileUploadAction"> <result name="success" type="dispatcher"> <param name="location">/fileupload/success.jsp</param> </result> <interceptor-ref name="params" /><!-- 将请求中的参数设置到Action中去 --> <interceptor-ref name="basicStack" /> <interceptor-ref name="uploadStack" /> <interceptor-ref name="model-driven" /> </action> </package> </xwork> 七、webwork.properties 属性文件; # 保存的目录缺省使用 javax.servlet.context.tempdir webwork.multipart.saveDir= javax.servlet.context.tempdir webwork.multipart.maxSize=4097152 # 文件上传设置 # 用来处理HTTP POST请求,编码使用MIME-type multipart/form-data方式的 webwork.multipart.parser=jakarta # 指定locale,编码 webwork.i18n.encoding=UTF-8 webwork.custom.i18n.resources=messages,acl,definition # 开发模式设定 webwork.devMode = false # 配置自动更新设定 webwork.configuration.xml.reload=true # 国际化资源设定 webwork.i18n.reload=true 访问方式:[url]http://localhost:8080/webworkupload/fileupload/upload.jsp[/url]]
转载请注明原文地址: https://www.6miu.com/read-4933480.html

最新回复(0)