Search in sources :

Example 1 with InvalidContentTypeException

use of org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException in project JessMA by ldcsaa.

the class FileUploader method upload.

/**
 * 执行上传
 *
 * @param request	: {@link HttpServletRequest} 对象
 * @param response	: {@link HttpServletResponse} 对象
 *
 * @return			: 成功:返回 {@link Result#SUCCESS} ,失败:返回其他结果,
 * 					     失败原因通过 {@link FileUploader#getCause()} 获取
 */
public Result upload(HttpServletRequest request, HttpServletResponse response) {
    reset();
    String absolutePath = getAbsoluteSavePath(request);
    if (absolutePath == null) {
        cause = new FileNotFoundException(String.format("path '%s' not found or is not directory", savePath));
        return Result.INVALID_SAVE_PATH;
    }
    ServletFileUpload sfu = getFileUploadComponent();
    List<FileItemInfo> fiis = new ArrayList<FileItemInfo>();
    List<FileItem> items = null;
    Result result = Result.SUCCESS;
    String encoding = servletHeaderencoding != null ? servletHeaderencoding : request.getCharacterEncoding();
    FileNameGenerator fnGenerator = fileNameGenerator != null ? fileNameGenerator : DEFAULT_FILE_NAME_GENERATOR;
    try {
        items = (List<FileItem>) sfu.parseRequest(request);
    } catch (FileUploadException e) {
        cause = e;
        if (e instanceof FileSizeLimitExceededException)
            result = Result.FILE_SIZE_EXCEEDED;
        else if (e instanceof SizeLimitExceededException)
            result = Result.SIZE_EXCEEDED;
        else if (e instanceof InvalidContentTypeException)
            result = Result.INVALID_CONTENT_TYPE;
        else if (e instanceof IOFileUploadException)
            result = Result.FILE_UPLOAD_IO_EXCEPTION;
        else
            result = Result.OTHER_PARSE_REQUEST_EXCEPTION;
    }
    if (result == Result.SUCCESS) {
        result = parseFileItems(items, fnGenerator, absolutePath, encoding, fiis);
        if (result == Result.SUCCESS)
            result = writeFiles(fiis);
    }
    return result;
}
Also used : InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) FileItem(org.apache.commons.fileupload.FileItem) SizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IOFileUploadException(org.apache.commons.fileupload.FileUploadBase.IOFileUploadException) IOFileUploadException(org.apache.commons.fileupload.FileUploadBase.IOFileUploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with InvalidContentTypeException

use of org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException in project eweb4j-framework by laiweiwei.

the class UploadUtil method handleUpload.

public static void handleUpload(Context context) throws Exception {
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    UploadConfigBean ucb = cb.getMvc().getUpload();
    String tmpDir = ucb.getTmp();
    int memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(ucb.getMaxMemorySize()) + "");
    long sizeMax = CommonUtil.parseFileSize(ucb.getMaxRequestSize());
    if (tmpDir.trim().length() == 0)
        tmpDir = "${RootPath}" + File.separator + "WEB-INF" + File.separator + "tmp";
    tmpDir = tmpDir.replace("${RootPath}", ConfigConstant.ROOT_PATH);
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(memoryMax);
    factory.setRepository(new File(tmpDir));
    ServletFileUpload _upload = new ServletFileUpload(factory);
    if (!_upload.isMultipartContent(context.getRequest()))
        return;
    _upload.setSizeMax(sizeMax);
    try {
        List<FileItem> items = _upload.parseRequest(context.getRequest());
        Iterator<FileItem> it = items.iterator();
        while (it.hasNext()) {
            FileItem item = it.next();
            String fieldName = item.getFieldName();
            if (item.isFormField()) {
                String value = item.getString();
                context.getQueryParamMap().put(fieldName, new String[] { value });
            } else {
                String fileName = item.getName();
                if (fileName == null || fileName.trim().length() == 0)
                    continue;
                String stamp = CommonUtil.getNowTime("yyyyMMddHHmmss");
                File tmpFile = new File(tmpDir + File.separator + stamp + "_" + fileName);
                item.write(tmpFile);
                UploadFile uploadFile = new UploadFile(tmpFile, fileName, fieldName, item.getSize(), item.getContentType());
                if (context.getUploadMap().containsKey(fieldName)) {
                    context.getUploadMap().get(fieldName).add(uploadFile);
                } else {
                    List<UploadFile> uploads = new ArrayList<UploadFile>();
                    uploads.add(uploadFile);
                    context.getUploadMap().put(fieldName, uploads);
                }
            }
        }
    } catch (InvalidContentTypeException e) {
        throw new Exception("upload file error", e);
    }
}
Also used : InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) ArrayList(java.util.ArrayList) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) File(java.io.File) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean)

Aggregations

ArrayList (java.util.ArrayList)2 FileItem (org.apache.commons.fileupload.FileItem)2 InvalidContentTypeException (org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileSizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException)1 IOFileUploadException (org.apache.commons.fileupload.FileUploadBase.IOFileUploadException)1 SizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 UploadConfigBean (org.eweb4j.config.bean.UploadConfigBean)1