如何使用spring mvc在本地存储中保存图像以及如何通过邮递员上传图像?

时间:2015-11-17 14:35:03

标签: java spring-mvc

我收到此错误:

  

请求被拒绝,因为没有找到多部分边界

当我通过邮递员上传图像时:我将saveImage作为url,在标题中我有内容/类型和multipart / form-data并选择raw并上传图像。

这是我的控制者:

@RequestMapping(value = "/saveImage",
 method = RequestMethod.POST, 
consumes = "application/json", produces = "application/json;charset=UTF-8")


    public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> getImage(
            @RequestBody(required = false) GenericRequestVO<ImageCriteriaVO> imageCriteriaVO,HttpServletRequest request, HttpServletResponse response ) {
            return  requestHandlerInvoker.callHandler(
                HandlerName.SAVE_IMAGE_HANDLER,request,response,imageCriteriaVO);
    }

这是我的经纪人:

public GenericResponseVO<? extends IServiceVO> handleRequest(
            UserContext userCtx, HttpServletRequest httpRequest,
            HttpServletResponse httpResponse,
            GenericRequestVO<? extends IServiceVO> requestVO)
            throws O2Exception {
        GenericResponseVO<ImageResultVO> imageVO = new GenericResponseVO<ImageResultVO>();
        ImageResultVO imageResultVO = (ImageResultVO) serviceInvoker
            .callService(ServiceName.IMAGE_SERVICE, userCtx,requestVO.getBody());
        imageVO.setBody(imageResultVO);
        imageVO.getHeader().setStatus(new Status(Status.SUCCESS, "Token"));         
        return imageVO;

这是我的服务:

public IServiceVO service(UserContext userCtx, IServiceVO inputVO)
            throws O2Exception {
        LOG.info(LoggingEvent.Image_INPROGRESS,"Inside get list of treasure hunt for corporate service");
        ImageCriteriaVO imageCriteriaVO = (ImageCriteriaVO) inputVO;
        System.out.println(imageCriteriaVO.getFile());
        ImageResultVO imageResultVO = new ImageResultVO();  
        CommonsMultipartFile file = null;           
            String fileName = null;
            if (!file.isEmpty()) {
                try {
                    fileName = file.getOriginalFilename();
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream buffStream = 
                            new BufferedOutputStream(new FileOutputStream(new File("F:/" + fileName)));
                    buffStream.write(bytes);
                    buffStream.close();
                }catch (Exception e) {

                }
            }   

        return imageResultVO;
    }

这是我的标准类:

public class ImageCriteriaVO implements IServiceVO{

    private byte[]  file;

    public byte[] getFile() {
        return file;
    }

    public void setFile(byte[] file) {
        this.file = file;
    }
}

1 个答案:

答案 0 :(得分:0)

我希望这个答案有所帮助

当您尝试上传时,从标题中删除Content-Type,Postman会自动执行此操作。

如果设置Content-Type: undefined,Postman会自动将Content-Type设置为multipart / form-data,并根据媒体设置边界。

Mail user agents ignore content-disposition headers in the messages.

文件名参数中应提及文件名。否则应在文件名和名称参数5中提及 multipart/form-data with Angular JS