1 18 19 package org.apache.struts.webapp.upload; 20 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.struts.action.Action; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionForward; 35 import org.apache.struts.action.ActionMapping; 36 import org.apache.struts.upload.FormFile; 37 38 39 40 48 49 50 public class UploadAction extends Action 51 { 52 public ActionForward execute(ActionMapping mapping, 53 ActionForm form, 54 HttpServletRequest request, 55 HttpServletResponse response) 56 throws Exception { 57 58 if (form instanceof UploadForm) { 59 60 String encoding = request.getCharacterEncoding(); 63 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) 64 { 65 response.setContentType("text/html; charset=utf-8"); 66 } 67 68 UploadForm theForm = (UploadForm) form; 69 70 String text = theForm.getTheText(); 72 73 String queryValue = theForm.getQueryParam(); 75 76 FormFile file = theForm.getTheFile(); 78 79 String fileName= file.getFileName(); 81 82 String contentType = file.getContentType(); 84 85 boolean writeFile = theForm.getWriteFile(); 86 87 String size = (file.getFileSize() + " bytes"); 89 90 String data = null; 91 92 try { 93 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 95 InputStream stream = file.getInputStream(); 96 if (!writeFile) { 97 if (file.getFileSize() < (4*1024000)) { 99 100 byte[] buffer = new byte[8192]; 101 int bytesRead = 0; 102 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { 103 baos.write(buffer, 0, bytesRead); 104 } 105 data = new String (baos.toByteArray()); 106 } 107 else { 108 data = new String ("The file is greater than 4MB, " + 109 " and has not been written to stream." + 110 " File Size: " + file.getFileSize() + " bytes. This is a" + 111 " limitation of this particular web application, hard-coded" + 112 " in org.apache.struts.webapp.upload.UploadAction"); 113 } 114 } 115 else { 116 OutputStream bos = new FileOutputStream (theForm.getFilePath()); 118 int bytesRead = 0; 119 byte[] buffer = new byte[8192]; 120 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { 121 bos.write(buffer, 0, bytesRead); 122 } 123 bos.close(); 124 data = "The file has been written to \"" + theForm.getFilePath() + "\""; 125 } 126 stream.close(); 128 } 129 catch (FileNotFoundException fnfe) { 130 return null; 131 } 132 catch (IOException ioe) { 133 return null; 134 } 135 136 request.setAttribute("text", text); 138 request.setAttribute("queryValue", queryValue); 139 request.setAttribute("fileName", fileName); 140 request.setAttribute("contentType", contentType); 141 request.setAttribute("size", size); 142 request.setAttribute("data", data); 143 144 file.destroy(); 146 147 return mapping.findForward("display"); 149 } 150 151 return null; 153 } 154 } | Popular Tags |