Search in sources :

Example 1 with FileService

use of com.bc.pmpheep.general.service.FileService in project pmph by BCSquad.

the class BinaryUploader method save.

public State save(HttpServletRequest request, Map<String, Object> conf) {
    FileItemStream fileStream = null;
    boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
    if (!ServletFileUpload.isMultipartContent(request)) {
        return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
    }
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    if (isAjaxUpload) {
        upload.setHeaderEncoding("UTF-8");
    }
    try {
        FileItemIterator iterator = upload.getItemIterator(request);
        while (iterator.hasNext()) {
            fileStream = iterator.next();
            if (!fileStream.isFormField()) {
                break;
            }
            fileStream = null;
        }
        if (fileStream == null) {
            return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
        }
        String savePath = (String) conf.get("savePath");
        String originFileName = fileStream.getName();
        String suffix = FileType.getSuffixByFilename(originFileName);
        originFileName = originFileName.substring(0, originFileName.length() - suffix.length());
        savePath = savePath + suffix;
        long maxSize = ((Long) conf.get("maxSize")).longValue();
        if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
            return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
        }
        savePath = PathFormat.parse(savePath, originFileName);
        String physicalPath = (String) conf.get("rootPath") + savePath;
        InputStream is = fileStream.openStream();
        State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize);
        is.close();
        if (storageState.isSuccess()) {
            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
            FileService fileService = (FileService) ctx.getBean("fileService");
            File file = FileUpload.getFileByFilePath(physicalPath);
            String picId = fileService.saveLocalFile(file, ImageType.SYS_MESSAGE, 1L);
            storageState.putInfo("url", "/image/" + picId);
            // storageState.putInfo("url", PathFormat.format(savePath));
            storageState.putInfo("type", suffix);
            storageState.putInfo("original", originFileName + suffix);
        }
        return storageState;
    } catch (FileUploadException e) {
        return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
    } catch (IOException e) {
        return new BaseState(false, AppInfo.IO_ERROR);
    }
}
Also used : FileService(com.bc.pmpheep.general.service.FileService) BaseState(com.bc.pmpheep.ueditor.define.BaseState) InputStream(java.io.InputStream) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ApplicationContext(org.springframework.context.ApplicationContext) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with FileService

use of com.bc.pmpheep.general.service.FileService in project pmph by BCSquad.

the class Base64Uploader method save.

public State save(String content, Map<String, Object> conf, HttpServletRequest request) {
    byte[] data = decode(content);
    long maxSize = ((Long) conf.get("maxSize")).longValue();
    if (!validSize(data, maxSize)) {
        return new BaseState(false, AppInfo.MAX_SIZE);
    }
    String suffix = FileType.getSuffix("JPG");
    String savePath = PathFormat.parse((String) conf.get("savePath"), (String) conf.get("filename"));
    savePath = savePath + suffix;
    String physicalPath = (String) conf.get("rootPath") + savePath;
    State storageState = StorageManager.saveBinaryFile(data, physicalPath);
    if (storageState.isSuccess()) {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
        FileService fileService = (FileService) ctx.getBean("fileService");
        File file = FileUpload.getFileByFilePath(physicalPath);
        String picId;
        try {
            picId = fileService.saveLocalFile(file, ImageType.SYS_MESSAGE, 1L);
        } catch (IOException e) {
            return new BaseState(false, AppInfo.IO_ERROR);
        }
        storageState.putInfo("url", "/image/" + picId);
        // storageState.putInfo("url", PathFormat.format(savePath));
        storageState.putInfo("type", suffix);
        storageState.putInfo("original", "");
    }
    return storageState;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) FileService(com.bc.pmpheep.general.service.FileService) BaseState(com.bc.pmpheep.ueditor.define.BaseState) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileService (com.bc.pmpheep.general.service.FileService)2 BaseState (com.bc.pmpheep.ueditor.define.BaseState)2 State (com.bc.pmpheep.ueditor.define.State)2 File (java.io.File)2 IOException (java.io.IOException)2 ApplicationContext (org.springframework.context.ApplicationContext)2 InputStream (java.io.InputStream)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1