Multipart transferTo looks for a wrong file address when using createTempFile

后端 未结 3 922
灰色年华
灰色年华 2021-01-22 01:04

I have the following java code in Spring framework to convert a multipart file to a regular file:

    public static File convertToFile(MultipartFile multipart) {         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 01:25

    So the code looks like this: StandardMultipartHttpServletRequest

    public void transferTo(File dest) throws IOException, IllegalStateException {
      this.part.write(dest.getPath());
    }
    

    and MultiPartInputStreamParser:

        /**
         * @see javax.servlet.http.Part#write(java.lang.String)
         */
        public void write(String fileName) throws IOException
        {
            if (_file == null)
            {
                _temporary = false;
    
                //part data is only in the ByteArrayOutputStream and never been written to disk
                _file = new File (_tmpDir, fileName);
    

    So in the case that the file isn't on disk, we will end up with /tmp twice.

    I think this is a bug in StandardMultipartHttpServletRequest -- it is passing a path to something which expects a filename.

提交回复
热议问题