001: package org.claros.intouch.webdisk.services;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import javax.servlet.ServletException;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.apache.commons.fileupload.FileItem;
014: import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
015: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
016: import org.apache.commons.fileupload.servlet.ServletFileUpload;
017: import org.claros.commons.configuration.PropertyFile;
018: import org.claros.commons.mail.utility.Utility;
019: import org.claros.intouch.common.services.BaseService;
020: import org.claros.intouch.common.utility.Constants;
021: import org.claros.intouch.webdisk.controllers.WebdiskController;
022:
023: public class UploadFileService extends BaseService {
024: private static final long serialVersionUID = 1635352720800214050L;
025: private static int MAX_SIZE;
026:
027: private static String tmpDir = Constants.tmpDir;
028:
029: static {
030: int maxSize = 5;
031: try {
032: maxSize = Integer.parseInt(PropertyFile.getConfiguration(
033: "/config/config.xml").getString(
034: "webdisk.upload-limit-size"));
035: } catch (Exception e) {
036: }
037: MAX_SIZE = 1024 * 1024 * maxSize;
038: }
039:
040: public void doGet(HttpServletRequest request,
041: HttpServletResponse response) throws ServletException,
042: IOException {
043: doPost(request, response);
044: }
045:
046: /**
047: * The doPost method of the servlet. <br>
048: *
049: * This method is called when a form has its tag value method equals to post.
050: *
051: * @param request the request send by the client to the server
052: * @param response the response send by the server to the client
053: * @throws ServletException if an error occurred
054: * @throws IOException if an error occurred
055: */
056: public void doPost(HttpServletRequest request,
057: HttpServletResponse response) throws ServletException,
058: IOException {
059: response.setHeader("Expires", "-1");
060: response.setHeader("Pragma", "no-cache");
061: response.setHeader("Cache-control", "no-cache");
062: response.setHeader("Content-Type", "text/html; charset=utf-8");
063:
064: request.setCharacterEncoding("UTF-8");
065:
066: ArrayList parts = (ArrayList) request.getSession()
067: .getAttribute("attachments");
068: if (parts == null) {
069: parts = new ArrayList();
070: }
071:
072: String fileName = null;
073: try {
074: // Create a factory for disk-based file items
075: DiskFileItemFactory factory = new DiskFileItemFactory();
076:
077: // Set factory constraints
078: factory.setRepository(new File(tmpDir));
079:
080: // Create a new file upload handler
081: ServletFileUpload upload = new ServletFileUpload(factory);
082:
083: // Set overall request size constraint
084: upload.setSizeMax(MAX_SIZE);
085:
086: // Parse the request
087: List items = upload.parseRequest(request);
088:
089: Iterator iter = items.iterator();
090: while (iter.hasNext()) {
091: FileItem item = (FileItem) iter.next();
092:
093: if (!item.isFormField()) {
094: //String fieldName = item.getFieldName();
095: fileName = item.getName();
096: // ie6 bug. it sends whole file path as file name. use firefox!
097: if (fileName.indexOf("\\") >= 0) {
098: fileName = fileName.substring(fileName
099: .lastIndexOf("\\") + 1);
100: }
101:
102: File uploadDir = WebdiskController
103: .getUploadDir(getAuthProfile(request)
104: .getUsername());
105: File uploadedFile = new File(uploadDir
106: .getAbsolutePath()
107: + "/" + fileName);
108: item.write(uploadedFile);
109: uploadedFile = null;
110:
111: if (fileName.indexOf("\\") > 0) {
112: fileName = fileName.substring(fileName
113: .lastIndexOf("\\") + 1);
114: }
115: item.delete();
116: }
117: }
118: response.sendRedirect("../upload_file_ok.jsp?result=0");
119: } catch (SizeLimitExceededException e) {
120: // attachment exceeded the attachment upload size limit
121: response
122: .sendRedirect("../upload_file_ok.jsp?result=1&maxAttSize="
123: + Utility.sizeToHumanReadable(MAX_SIZE));
124: } catch (Exception e) {
125: response.sendRedirect("../upload_file_ok.jsp?result=3");
126: }
127: }
128: }
|