1 17 package org.alfresco.web.app.servlet; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.http.HttpSession ; 28 29 import org.alfresco.error.AlfrescoRuntimeException; 30 import org.alfresco.util.TempFileProvider; 31 import org.alfresco.web.app.Application; 32 import org.alfresco.web.bean.FileUploadBean; 33 import org.apache.commons.fileupload.FileItem; 34 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 35 import org.apache.commons.fileupload.servlet.ServletFileUpload; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 45 public class UploadFileServlet extends BaseServlet 46 { 47 private static final long serialVersionUID = -5482538466491052873L; 48 private static Log logger = LogFactory.getLog(UploadFileServlet.class); 49 50 53 protected void service(HttpServletRequest request, HttpServletResponse response) 54 throws ServletException , IOException 55 { 56 String returnPage = null; 57 boolean isMultipart = ServletFileUpload.isMultipartContent(request); 58 59 try 60 { 61 AuthenticationStatus status = servletAuthenticate(request, response); 62 if (status == AuthenticationStatus.Failure) 63 { 64 return; 65 } 66 67 if (isMultipart == false) 68 { 69 throw new AlfrescoRuntimeException("This servlet can only be used to handle file upload requests, make" + 70 "sure you have set the enctype attribute on your form to multipart/form-data"); 71 } 72 73 if (logger.isDebugEnabled()) 74 logger.debug("Uploading servlet servicing..."); 75 76 HttpSession session = request.getSession(); 77 ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); 78 79 upload.setHeaderEncoding("UTF-8"); 81 82 List <FileItem> fileItems = upload.parseRequest(request); 83 84 Iterator <FileItem> iter = fileItems.iterator(); 85 FileUploadBean bean = new FileUploadBean(); 86 while(iter.hasNext()) 87 { 88 FileItem item = iter.next(); 89 if(item.isFormField()) 90 { 91 if (item.getFieldName().equalsIgnoreCase("return-page")) 92 { 93 returnPage = item.getString(); 94 } 95 } 96 else 97 { 98 String filename = item.getName(); 99 if (filename != null && filename.length() != 0) 100 { 101 if (logger.isDebugEnabled()) 102 logger.debug("Processing uploaded file: " + filename); 103 104 int idx = filename.lastIndexOf('\\'); 107 108 if (idx == -1) 109 { 110 idx = filename.lastIndexOf('/'); 112 } 113 114 if (idx != -1) 115 { 116 filename = filename.substring(idx + File.separator.length()); 117 } 118 119 File tempFile = TempFileProvider.createTempFile("alfresco", ".upload"); 120 item.write(tempFile); 121 bean.setFile(tempFile); 122 bean.setFileName(filename); 123 bean.setFilePath(tempFile.getAbsolutePath()); 124 session.setAttribute(FileUploadBean.FILE_UPLOAD_BEAN_NAME, bean); 125 if (logger.isDebugEnabled()) 126 logger.debug("Temp file: " + tempFile.getAbsolutePath() + " created from upload filename: " + filename); 127 } 128 } 129 } 130 131 if (returnPage == null || returnPage.length() == 0) 132 { 133 throw new AlfrescoRuntimeException("return-page parameter has not been supplied"); 134 } 135 136 if (logger.isDebugEnabled()) 138 logger.debug("Upload servicing complete, redirecting to: " + returnPage); 139 140 response.sendRedirect(returnPage); 141 } 142 catch (Throwable error) 143 { 144 Application.handleServletError(getServletContext(), (HttpServletRequest )request, 145 (HttpServletResponse )response, error, logger, returnPage); 146 } 147 } 148 } 149 | Popular Tags |