package com.snail.example; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { private static final long serialVersionUID = 8531510541714915232L; private FormFile upFile; private String comment; public void setComment(String comment) { this.comment = comment; } public String getComment() { return comment; } public void setUpFile(FormFile upFile) { this.upFile = upFile; } public FormFile getUpFile() { return upFile; } }
複数ファイルをアップロードしたいときには、FormFile?[] にする。
package com.snail.example; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class UploadAction extends Action { public ActionForward execute(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res){ UploadForm fm = (UploadForm)form; String comment = fm.getComment(); FormFile file = (FormFile)fm.getUpFile(); String contentType = file.getContentType(); String fileName = file.getFileName(); int size = file.getFileSize(); req.setAttribute("FileName",fileName); req.setAttribute("FileSize",size); req.setAttribute("ContentType", contentType); req.setAttribute("Comment", comment); if("application/zip".equals(contentType)){ try{ String deflatedFiles = inflateZip(file.getInputStream()); req.setAttribute("Deflate",deflatedFiles); }catch (Exception e) { e.printStackTrace(); req.setAttribute("Deflate", "failed"); }finally{ file.destroy(); } } return map.findForward("success"); } private String inflateZip(InputStream is) throws FileNotFoundException, IOException{ StringBuilder outFileNames = new StringBuilder(); ZipInputStream zipInp = new ZipInputStream(new BufferedInputStream(is)); ZipEntry entry = null; while ((entry = zipInp.getNextEntry()) != null) { if (!entry.isDirectory()) { outFileNames.append(entry .getName()); CheckedOutputStream out = new CheckedOutputStream( new BufferedOutputStream(new FileOutputStream("C:\\out\\"+entry .getName())), new CRC32()); byte[] buf = new byte[zipInp.available()]; int writeSize = 0; int totalSize = 0; while ((writeSize = zipInp.read(buf)) != -1) { totalSize += writeSize; out.write(buf, 0, writeSize); } out.close(); if (entry.getSize() != totalSize) { System.out.println("wrong size"); } if (entry.getCrc() != out.getChecksum().getValue()) { System.out.println("wrong checksum"); } } zipInp.closeEntry(); } zipInp.close(); return outFileNames.toString(); } }
if("application/zip".equals(contentType)){は、
if(contentType.matches(".*zip.*")){の方が良さげ。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="uploadForm" type="com.snail.example.UploadForm"/> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> <forward name="welcome" path="/Welcome.do"/> </global-forwards> <action-mappings> <action path="/Welcome" forward="/pages/Welcome.jsp"> </action> <action path="/upload" name="uploadForm" type="com.snail.example.UploadAction" scope="request" validate="false" input="/pages/Welcome.jsp"> <forward name="success" path="/pages/done.jsp"/> </action> </action-mappings> <controller bufferSize="4096" maxFileSize="256M" tempDir="C:\temp"/> <message-resources parameter="java.MessageResources"/> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> </struts-config>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <html:html> <head> <title>Upload Example</title> <html:base/> </head> <body bgcolor="white"> <html:form action="upload.do" enctype="multipart/form-data"> <table> <tr> <td>file:</td> <td><html:file property="upFile"/></td> </tr> <tr> <td>comment:</td> <td><html:text property="comment"/></td> </tr> <tr> <td colspan="2" align="right"> <html:submit>Upload</html:submit> </td> </tr> </table> </html:form> </body> </html:html>
enctype を指定することにより、form(ここでは"comment")と添付ファイルを同時に送られるようになる。
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <html:html> <head> <title>Complete</title> <html:base/> </head> <body bgcolor="white"> Update Done. <table> <tr> <td>File Name</td> <td><bean:write name="FileName"/></td> </tr> <tr> <td>File Size</td> <td><bean:write name="FileSize"/></td> </tr> <tr> <td>Content Type</td> <td><bean:write name="ContentType"/></td> </tr> <tr> <td>Comment</td> <td><bean:write name="Comment"/></td> </tr> <tr> <td>Zip Contents</td> <td><bean:write name="Deflate"/></td> </tr> </table> </body> </html:html>