mongodb找不到档案

时间:2015-08-19 22:45:15

标签: mongodb spring-boot spring-data

我有一个spring-boot应用程序充当图像服务器。它会收到如下图像:

package data;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;
public class Boot {
    public Boot()
    {

        Display.setTitle("A.T's game");
        try {
            Display.setDisplayMode(new DisplayMode (640, 480));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,640,480,0,1,-1);
        glMatrixMode(GL_MODELVIEW);

        while(!Display.isCloseRequested())
        {
            glBegin(GL_LINES);
            glVertex2f(35,35);
            glVertex2f(53,53);
            glEnd();
        }
        Display.destroy();
    }
    public static void main(String[] args)
    {
        new Boot() ;
    }

}

然后调用:

 @RequestMapping(value = "images/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity addPortrait(@RequestParam MultipartFile file, @PathVariable Long patientId) throws Exception {

        try {
            GridFSFile storedFile = mongoFileService.add( file, id );
            LOGGER.info( "Returning Filename " + storedFile.getFilename() );
            return ResponseEntity.ok().contentType( MediaType.APPLICATION_JSON )
                    .body(  SERVER_URL+ storedFile.getFilename() );

        } catch ( Exception ex ) {
            ex.printStackTrace();
            LOGGER.error("Error storing file " + file.getOriginalFilename(), ex );
            return ResponseEntity.status( HttpStatus.INTERNAL_SERVER_ERROR )
                    .contentType( MediaType.APPLICATION_JSON).body(MISSING_IMAGE_PATH );

        }
    }

返回的结果是正确的,我可以在MongoDB中看到该文件:

private GridFsOperations gridFsOperations;

public GridFSFile add(MultipartFile file, Long patientId) throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append(System.currentTimeMillis());
    sb.append("_");
    sb.append(file.getOriginalFilename());
    return gridFsOperations.store( file.getInputStream(), sb.toString() );
    // return gridFsAppStore.store(file, sb.toString());
}

enter image description here

所以我很确定PUT调用有效。

但是,当我尝试使用控制器检索该图像时:

db.getCollection('fs.files').find({}):

获取图像的MongoFileService是:

@RequestMapping(value = "images/{filename}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getSizedImage(@PathVariable String filename, @RequestParam int width, @RequestParam int height) throws Exception {
        if (savedFile != null) {
            try {
               BufferedImage image = ImageIO.read( savedFile.getInputStream() );
               image = resize( image, Method.SPEED, width, height, Scalr.OP_ANTIALIAS );

               LOGGER.info( "Returning Filename " + savedFile.getFilename() + " sized to " + width + " X " + height );
               return ResponseEntity.ok().contentLength( savedFile.getLength() )
                    .contentType( MediaType.IMAGE_JPEG ).body( image );
          } catch (Exception ex) {
            ex.printStackTrace();
            LOGGER.error("Error sizing file " + filename + ": " + ex.getMessage());
              return ResponseEntity.status( HttpStatus.INTERNAL_SERVER_ERROR )
                    .contentType( MediaType.APPLICATION_JSON).body("Error sizing file " + filename + ": " + ex.getMessage() );
           }
       } else {
        LOGGER.error("Could not find requested file " + filename );
        return ResponseEntity.status( HttpStatus.NOT_FOUND )
                        .contentType( MediaType.APPLICATION_JSON).body(MISSING_IMAGE_PATH );
    }
    }

}

我一无所获。除了以下内容之外,日志中没有错误,没有图像或任何内容:

@Component
public class MongoFileService {

     @Autowired
     private GridFsOperations gridFsOperations;

      public GridFSDBFile getStore(String filename) throws IOException {

           Query query = new Query( GridFsCriteria.whereFilename().is( filename ) );
    return gridFsOperations.findOne( query );
      }

      public GridFSFile add(MultipartFile file, Long id) throws IOException {

          StringBuilder sb = new StringBuilder();
          sb.append(System.currentTimeMillis());
          sb.append("_");
          sb.append(file.getOriginalFilename());
          return gridFsOperations.store( file.getInputStream(), sb.toString() );
      }

我正在使用Spring-Boot 1.3.0.BUILD-SNAPSHOT,MongoDB 2.4.14,Spring-Data。

有人能看出为什么这个结果没有被退回吗?

已更新以显示MongoFileService 我在控制器中设置了一个断点,收到的文件名为" 1440128243370_IMG_3415"这是错过了" JPG"延期。如果我手动更改调试器中的值,则返回该文件。

为了解决这个问题,我在项目中添加了以下内容:

"Error retrieving file " and the file name parameter provided

1 个答案:

答案 0 :(得分:0)

一种可能的方法是更改​​@RequestMapping中相关方法的URI模板变量匹配。语法为{varName:regex}。要使{filename}变量与所有内容(包括点和文件扩展名)匹配,请使用以下定义:

@RequestMapping(value = "images/{filename:.+}", method = RequestMethod.GET)

更多详细信息,请参阅documentation