Search in sources :

Example 31 with DiskFileItemFactory

use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project MSEC by Tencent.

the class FileUploadServlet method FileUpload.

// 处理multi-part格式的http请求
// 将key-value字段放到fields里返回
// 将文件保存到tmp目录,并将文件名保存到filesOnServer列表里返回
protected static String FileUpload(Map<String, String> fields, List<String> filesOnServer, HttpServletRequest request, HttpServletResponse response) {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    int MaxMemorySize = 200000000;
    int MaxRequestSize = MaxMemorySize;
    String tmpDir = System.getProperty("TMP", "/tmp");
    factory.setSizeThreshold(MaxMemorySize);
    factory.setRepository(new File(tmpDir));
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding("utf8");
    upload.setSizeMax(MaxRequestSize);
    try {
        List<FileItem> items = upload.parseRequest(request);
        // Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (item.isFormField()) {
                // 普通的k -v字段
                String name = item.getFieldName();
                String value = item.getString("utf-8");
                fields.put(name, value);
            } else {
                String fieldName = item.getFieldName();
                String fileName = item.getName();
                if (fileName == null || fileName.length() < 1) {
                    return "file name is empty.";
                }
                String localFileName = ServletConfig.fileServerRootDir + File.separator + "tmp" + File.separator + fileName;
                String contentType = item.getContentType();
                boolean isInMemory = item.isInMemory();
                long sizeInBytes = item.getSize();
                File uploadedFile = new File(localFileName);
                item.write(uploadedFile);
                filesOnServer.add(localFileName);
            }
        }
        return "success";
    } catch (FileUploadException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 32 with DiskFileItemFactory

use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project MSEC by Tencent.

the class FileUploadServlet method FileUpload.

// 处理multi-part格式的http请求
// 将key-value字段放到fields里返回
// 将文件保存到tmp目录,并将文件名保存到filesOnServer列表里返回
protected static String FileUpload(Map<String, String> fields, List<String> filesOnServer, HttpServletRequest request, HttpServletResponse response) {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    int MaxMemorySize = 10000000;
    int MaxRequestSize = MaxMemorySize;
    String tmpDir = System.getProperty("TMP", "/tmp");
    // System.out.printf("temporary directory:%s", tmpDir);
    // Set factory constraints
    factory.setSizeThreshold(MaxMemorySize);
    factory.setRepository(new File(tmpDir));
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding("utf8");
    // Set overall request size constraint
    upload.setSizeMax(MaxRequestSize);
    // Parse the request
    try {
        @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(request);
        // Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (item.isFormField()) {
                // 普通的k -v字段
                String name = item.getFieldName();
                String value = item.getString("utf-8");
                fields.put(name, value);
            } else {
                String fieldName = item.getFieldName();
                String fileName = item.getName();
                if (fileName == null || fileName.length() < 1) {
                    return "file name is empty.";
                }
                String localFileName = ServletConfig.fileServerRootDir + File.separator + "tmp" + File.separator + fileName;
                // System.out.printf("upload file:%s", localFileName);
                String contentType = item.getContentType();
                boolean isInMemory = item.isInMemory();
                long sizeInBytes = item.getSize();
                File uploadedFile = new File(localFileName);
                item.write(uploadedFile);
                filesOnServer.add(localFileName);
            }
        }
        return "success";
    } catch (FileUploadException e) {
        e.printStackTrace();
        return e.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return e.toString();
    }
}
Also used : DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 33 with DiskFileItemFactory

use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project zm-mailbox by Zimbra.

the class FileUploadServlet method getUploader.

public static ServletFileUpload getUploader(long maxSize) {
    DiskFileItemFactory dfif = new DiskFileItemFactory();
    dfif.setSizeThreshold(32 * 1024);
    dfif.setRepository(new File(getUploadDir()));
    ServletFileUpload upload = new ServletFileUpload(dfif);
    upload.setSizeMax(maxSize);
    upload.setHeaderEncoding("utf-8");
    return upload;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) File(java.io.File)

Example 34 with DiskFileItemFactory

use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project MassBank-web by MassBank.

the class AdminActions method doUpload.

@Action(name = "doUpload", post = true)
public Redirect doUpload(HttpServletRequest req) throws ServletException {
    RequestContext reqContext = new ServletRequestContext(req);
    boolean isMultipart = ServletFileUpload.isMultipartContent(reqContext);
    if (isMultipart) {
        try {
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<?> items = upload.parseRequest(req);
            // Process the uploaded items
            Iterator<?> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String fileName = item.getName();
                    String fileExtesion = fileName;
                    fileExtesion = fileExtesion.toLowerCase();
                    if (!(fileExtesion.endsWith(".jar") || fileExtesion.endsWith(".aar"))) {
                        return new Redirect(UPLOAD).withStatus(false, "Unsupported file type " + fileExtesion);
                    } else {
                        String fileNameOnly;
                        if (fileName.indexOf("\\") < 0) {
                            fileNameOnly = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
                        } else {
                            fileNameOnly = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
                        }
                        File uploadedFile = new File(serviceDir, fileNameOnly);
                        item.write(uploadedFile);
                        return new Redirect(UPLOAD).withStatus(true, "File " + fileNameOnly + " successfully uploaded");
                    }
                }
            }
        } catch (Exception e) {
            return new Redirect(UPLOAD).withStatus(false, "The following error occurred: " + e.getMessage());
        }
    }
    throw new ServletException("Invalid request");
}
Also used : ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) RequestContext(org.apache.commons.fileupload.RequestContext) File(java.io.File)

Example 35 with DiskFileItemFactory

use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project mica2 by obiba.

the class DraftStudiesImportResource method getUploadedFile.

/**
 * Returns the first {@code FileItem} that is represents a file upload field. If no such field exists, this method
 * returns null
 *
 * @param request
 * @return
 * @throws FileUploadException
 */
FileItem getUploadedFile(HttpServletRequest request) throws FileUploadException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    for (FileItem fileItem : upload.parseRequest(request)) {
        if (!fileItem.isFormField()) {
            return fileItem;
        }
    }
    return null;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Aggregations

DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)90 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)84 FileItem (org.apache.commons.fileupload.FileItem)72 FileUploadException (org.apache.commons.fileupload.FileUploadException)48 File (java.io.File)44 IOException (java.io.IOException)31 HashMap (java.util.HashMap)24 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)24 List (java.util.List)21 ArrayList (java.util.ArrayList)20 InputStream (java.io.InputStream)17 ServletException (javax.servlet.ServletException)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)9 Locale (java.util.Locale)8 JSONObject (org.json.JSONObject)8 ApplicationContext (org.springframework.context.ApplicationContext)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 Iterator (java.util.Iterator)7 Map (java.util.Map)7