上载应用程序/八位字节流(图像JPG-Spring Multipart帖子请求)

时间:2019-05-13 12:48:55

标签: spring image file-upload format ionic4

我正在尝试通过移动应用程序在我的服务器上存储图片(jpg)

我用离子照相机(CameraPreview)拍摄图像,并通过post http方法将其传递到Back服务(春季)。

  

问题是格式。图片已正确存储,但没有任何格式

这是拍照的相机代码:

 takePicture() {
    this.cameraPreview.takePicture(this.pictureOpts).then((imageData) => {
     this.selectedImage = 'data:image/jpeg;base64,' + imageData;
      this.globalDataService.changePictureTaken(this.selectedImage);
      this.location.back();
    }, (err) => {
      console.log(err);
      this.selectedImage = 'assets/img/test.jpg';
    });
  }

这是将图像发送到服务器的代码

uploadImageService(url: string, param: any) {
    const fd: FormData = new FormData();
    const imgBlob = new Blob([param],{type: param.type});
    fd.append('file', imgBlob);
     return this.http
       .post(url, fd, httpOptionsImages);
  }

这是服务器端代码:

 @RequestMapping(method = RequestMethod.POST, value = "/subir-imagen")
    public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) throws IOException, SQLException {
        LOGGER.log(Level.INFO, "/subir-imagen, handleFileUpload file.tostring {0}", file.toString());
 String fileName = fileManagerService.storeFile(file);
 String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/downloadFile/")
                .path(fileName)
                .toUriString()
        return ResponseEntity.ok(fileDownloadUri);
}

public String storeFile(MultipartFile file) throws SQLException, IOException {
        // Normalize file name
        String fileName = StringUtils.cleanPath(file.getName());
        try {
            // Check if the file's name contains invalid characters
            if (fileName.contains("..")) {
                LOGGER.log(Level.INFO, "Sorry! Filename contains invalid path sequence {0}", fileName);
            }
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            Path path = Paths.get(fileStoragePath + File.separator + fileName + "." + extension.toLowerCase());
            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
       } 
       catch (IOException ex) {
            LOGGER.log(Level.INFO, "Could not store file " + fileName + ". Please try again! {0}", ex);
       }
    return fileName;
}

当我通过Postman发出发帖请求时,图像以正确的格式存储,并且在下载时,图像会正确显示。

当我通过应用程序发出请求时,图像已正确存储在服务器中,但是没有扩展名,我尝试手工放置格式,但无法显示。

当我在服务器端file.getContentType());中运行时,我已经看到我的应用程序发送的图像是 application / octet-stream 类型,而邮递员发送的图像是图片/ jpg

我尝试将应用程序/八位字节流转换为image / jpg,但在Java中没有成功。 我也尝试将Content-Type标头添加到请求中,但是我只有一些错误...

我还尝试过将图像直接通过表单传递,没有斑点,也没有斑点或形式,但仍然无法正常工作。

复制文件时,有什么方法可以将其传递给具体形式? 或通过离子照相机格式化该图像的任何方式?

谢谢!

1 个答案:

答案 0 :(得分:0)

也许这里有两个独立的问题。返回的图像数据是 base64编码的 JPEG,当以data:image/jpeg;base64,开头时,适用于Web视图。但是普通的图片查看器(和大多数其他应用程序)期望使用 binary JPEG(未进行base64编码),并且不带data:image标签。

我们可以通过使用十六进制或文本编辑器打开服务器上的文件来检查它是否为base64编码。可以通过在文件开头附近添加字符串“ JFIF”来识别二进制JPEG。

如果是这样,首先要尝试的是将base64映像转换为二进制格式(不包含“ data:image”),然后再将其发送到服务器。或者我们也可以转换服务器端。使用Javascript可以使用atob()函数,而在服务器上Java 8具有Base64库。还有来自Apache Commons的编码器/解码器(codec)。

可以通过为fd.append()指定完整的文件名来解决文件扩展名。

相关问题