nginx + ttomcat+mongodb upload file problem


nginx:1.7.7
tomcat:8.0.3
mongodb:3

nginx+tomcat+mongodb The development file storage server
  found that the file failed to obtain the file after the file was uploaded, and the file upload was suspected
  to

  have failed. , and then avoid nginx and try to
  get files directly through tomcat and find that it is OK. At this time, it is suspected that there is a problem with the nginx proxy service.

  Modify the nginx configuration file, join the upstream rotation training, and find that it is still not possible.
  Check the nginx log and find that it is a file permission problem. Change it to root or other users, and
  restart the nginx service directly as root or other users. Everything is OK.

The summary is as follows: mongodb does not need other storage files, unless there are special requirements, the
upload size is limited to nginx client_max_body_size 200M; configuration.
Check whether the upload is successful or not. Directly check whether the file exists in the files collection, and check whether there is a file block in chunks. If it exists, the upload is successful.



save document
public RtResponse saveFile(RtRequest request) throws Exception {
        logger.info("Enter attachment upload interface....");

        RtResponse response = new RtResponse();

        Map<String, Object> propers = request.getAttr();

        byte[] byt = (byte[]) propers.get("file");
        String ticket = (String) propers.get("ticket");
        if (AppUtils.isBlank (ticket))
            response.setCode(ResultCode.NOT_LOGIN);
            response.setMsg("User is not logged in");
            return response;
        }
        final TokenVo token = (TokenVo) ssoCacheUtil.getObject(ticket);
        if (AppUtils.isBlank(token)) {
            response.setCode(ResultCode.NOT_LOGIN);
            response.setMsg("User is not logged in");
            return response;
        }

        InputStream input = new ByteArrayInputStream(byt);

        String contentType = (String) propers.get("contentType");
        String fileName = (String) propers.get("fileName");

        logger.info("Attachment upload interface file stream content...{}", input);
        String fileId = UUIDUtil.getKeys();

        GridFS photo = new GridFS(mongotemplate.getDb(), "file_collections");
        GridFSInputFile fp = photo.createFile(input);
        fp.setFilename(fileName);
        fp.setId(fileId);
        fp.setContentType(contentType);
        fp.save();
        
      
        
        AttachmentVO attach = new AttachmentVO();
        attach.setContentType(contentType);
        attach.setFileId(fileId);
        attach.setFileName(fileName);

        response.setData(attach);
        response.setCode(ResultCode.SUCCESS);

        return response;
    }


download file

@RequestMapping(value = "/{project}/{moduel}/{download}/{meth}/{fileId}", method = RequestMethod.GET)
    public @ResponseBody void download(HttpServletRequest request, HttpServletResponse hTresponse,
            @PathVariable("project") String project, @PathVariable("moduel") String moduel,
            @PathVariable("meth") String meth, @PathVariable("download") String download,
            @PathVariable("fileId") String fileId) throws IOException {

        LOG.info(request.getRequestURI());
        String baseUrl = String.format("/%s/%s/%s/%s", project, moduel, download, meth);

        RtResponse response = reqUrlDefService.downloadAttachment(baseUrl, request, fileId);

        Map<String, Object> data = (Map<String, Object>) response.getData();

        String fileName = (String) data.get("fileName");
        String contentType = (String) data.get("contentType");
        byte[] byt = (byte[]) data.get("fileByte");

        String filename = URLEncoder.encode(fileName, "UTF-8");

        hTresponse.setContentType(contentType);
        hTresponse.setHeader("Content-disposition", "attachment;filename=" + filename);

        OutputStream os = hTresponse.getOutputStream();
        os.write(byt);
        os.flush();
        os.close();

    }



public RtResponse findOneFile(RtRequest request) throws Exception {
        logger.info("Enter the attachment acquisition interface....");

        RtResponse response = new RtResponse();

        Map<String, Object> propers = request.getAttr();

        String fileId = (String) propers.get("fileId");
        if (AppUtils.isBlank (fileId)) {
        	response.setCode(ResultCode.FIELD_ID_IS_NULL);
            response.setMsg("Attachment ID cannot be empty");
            return response;
        }

        GridFS gridFS = new GridFS(mongotemplate.getDb(), "file_collections");
        DBObject query = new BasicDBObject("_id", fileId);
        GridFSDBFile gridFSDBFile = gridFS.findOne(query);
        
        if (AppUtils.isBlank (gridFSDBFile)) {
        	response.setCode(ResultCode.ATTACH_MENT_IS_NULL);
            response.setMsg("Attachment does not exist");
            return response;
        }

        logger.info("Accessory interface file stream content...{}", gridFSDBFile.getInputStream());
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("fileName", gridFSDBFile.getFilename());
        maps.put("contentType", gridFSDBFile.getContentType());
        maps.put("fileByte", gridFSDBFile.getInputStream());

        response.setCode(ResultCode.SUCCESS);
        response.setData(maps);

        return response;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326421873&siteId=291194637