1 15 16 package com.jdon.strutsutil.file; 17 18 import java.io.*; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import org.apache.struts.action.Action; 22 import org.apache.struts.action.ActionForm; 23 import org.apache.struts.action.ActionForward; 24 import org.apache.struts.action.ActionMapping; 25 import org.apache.struts.upload.FormFile; 26 27 import com.jdon.strutsutil.file.filter.*; 28 29 41 public class MultiUploadAction extends Action { 42 43 public ActionForward execute(ActionMapping mapping, 44 ActionForm form, 45 HttpServletRequest request, 46 HttpServletResponse response) throws Exception { 47 48 if (form == null) { 49 throw new Exception ("please setup UploadImageForm in struts-config.xml"); 50 } 51 MultiImageForm theForm = (MultiImageForm) form; 52 FormFile[] files = theForm.getFiles(); 53 int length = files.length; 54 55 ImageSessionFilter imageFilter = new ImageSessionFilter(); 56 57 UploadFile uploadFile = null; 58 for (int i = 0; i < length; i++) { 59 uploadFile = doFormFile(request, files[i]); 60 uploadFile.setName(theForm.getNames()[i]); 61 imageFilter.addUploadFile(request, uploadFile); 63 } 64 65 return mapping.findForward("display"); 67 68 } 69 70 private UploadFile doFormFile(HttpServletRequest request, FormFile file) throws 71 Exception { 72 UploadFile uploadFile = new UploadFile(); 73 try { 74 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 76 InputStream stream = file.getInputStream(); 77 78 byte[] buffer = new byte[8192]; 79 int bytesRead = 0; 80 while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) { 81 baos.write(buffer, 0, bytesRead); 82 } 83 84 byte[] data = baos.toByteArray(); 85 baos.close(); 86 stream.close(); 88 89 if (data != null) { 90 uploadFile.setData(data); 91 uploadFile.setContentType(file.getContentType()); 92 uploadFile.setSize(file.getFileSize()); 93 94 } 95 } catch (Exception ex) { 96 throw new Exception (ex); 97 } finally { 98 file.destroy(); 100 } 101 102 return uploadFile; 103 } 104 105 } 106 | Popular Tags |