上传文件并直接将其传输到磁盘而不存储在内存中

时间:2022-07-02 21:42:56

I have a controller which allows me to upload a file via PUT. The file content (byte[]) is first stored in memory and only then written to the disk.

我有一个控制器,允许我通过PUT上传文件。文件内容(byte [])首先存储在内存中,然后才写入磁盘。

Is there a way to stream the file directly to the disk without storing it in memory ?

有没有办法将文件直接流式传输到磁盘而不将其存储在内存中?

The code is here:

代码在这里:

package dummy;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class WebController {

    @RequestMapping(value = "/upload", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void addCardproviderLogo(final HttpEntity<byte[]> requestEntity) throws IOException {

            final byte[] payload = requestEntity.getBody();

            FileUtils.writeByteArrayToFile(new File("/tmp/myfile-" + System.currentTimeMillis() + ".tmp"), payload);
    }

}

1 个解决方案

#1


you could use MultipartHttpServletRequest, than you can extract the CommonsMultipartFile that contain the inputStream to persist on your database

你可以使用MultipartHttpServletRequest,而不是你可以提取包含inputStream的CommonsMultipartFile来保存在你的数据库上

 public Map uploadFile(MultipartHttpServletRequest defaultMultipartHttpServletRequest,
                               @PathVariable(CommonConstants.PUBLIC_PROCUREMENT_ID) Integer id,
                               @PathVariable(CommonConstants.PARCEL_ID) Integer parcelId) {
    LOGGER.debug("upload item file for public procurement {}", id);
    CommonsMultipartFile file = (CommonsMultipartFile) (defaultMultipartHttpServletRequest.getFile(FILE_DATA) == null ?
            defaultMultipartHttpServletRequest.getFile(FILE_DATA_EXPLORER) : defaultMultipartHttpServletRequest.getFile(FILE_DATA));

 }

#1


you could use MultipartHttpServletRequest, than you can extract the CommonsMultipartFile that contain the inputStream to persist on your database

你可以使用MultipartHttpServletRequest,而不是你可以提取包含inputStream的CommonsMultipartFile来保存在你的数据库上

 public Map uploadFile(MultipartHttpServletRequest defaultMultipartHttpServletRequest,
                               @PathVariable(CommonConstants.PUBLIC_PROCUREMENT_ID) Integer id,
                               @PathVariable(CommonConstants.PARCEL_ID) Integer parcelId) {
    LOGGER.debug("upload item file for public procurement {}", id);
    CommonsMultipartFile file = (CommonsMultipartFile) (defaultMultipartHttpServletRequest.getFile(FILE_DATA) == null ?
            defaultMultipartHttpServletRequest.getFile(FILE_DATA_EXPLORER) : defaultMultipartHttpServletRequest.getFile(FILE_DATA));

 }