Search in sources :

Example 1 with JSONView

use of ubic.gemma.web.view.JSONView in project Gemma by PavlidisLab.

the class FileUploadController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
    if (!(request instanceof MultipartHttpServletRequest)) {
        return null;
    }
    Map<String, Object> model = new HashMap<>();
    if (request instanceof FailedMultipartHttpServletRequest) {
        String errorMessage = ((FailedMultipartHttpServletRequest) request).getErrorMessage();
        model.put("success", false);
        model.put("error", errorMessage);
    } else {
        MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = mrequest.getFileMap();
        if (fileMap.size() > 1) {
            FileUploadController.log.error("Attempted to upload multiple files, returning error");
            model.put("success", false);
            model.put("error", "Sorry, can't upload more than one file at a time yet");
        }
        for (String key : fileMap.keySet()) {
            MultipartFile multipartFile = fileMap.get(key);
            File copiedFile = null;
            try {
                copiedFile = FileUploadUtil.copyUploadedFile(multipartFile, request);
                FileUploadController.log.info("Uploaded file: " + copiedFile);
                model.put("success", true);
                model.put("localFile", StringEscapeUtils.escapeJava(copiedFile.getAbsolutePath()));
                model.put("originalFile", multipartFile.getOriginalFilename());
                model.put("size", multipartFile.getSize());
            } catch (Exception e) {
                FileUploadController.log.error("Error in upload: " + e.getMessage(), e);
                model.put("success", false);
                model.put("error", e.getMessage());
            }
            if (copiedFile == null) {
                FileUploadController.log.error("Error in upload: unknown problem getting file");
                model.put("success", false);
                model.put("error", "unknown problem getting file");
            }
        }
    }
    return new ModelAndView(new JSONView("text/html; charset=utf-8"), model);
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) JSONView(ubic.gemma.web.view.JSONView) FailedMultipartHttpServletRequest(ubic.gemma.web.util.upload.FailedMultipartHttpServletRequest) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) FailedMultipartHttpServletRequest(ubic.gemma.web.util.upload.FailedMultipartHttpServletRequest)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 FailedMultipartHttpServletRequest (ubic.gemma.web.util.upload.FailedMultipartHttpServletRequest)1 JSONView (ubic.gemma.web.view.JSONView)1