File upload 10kb step on the pit record

Overview

需要开发一个专门用来上传文件的组件图档功能,主要是用来上传各个工单的附件,上传到服务器ftp上。

demand

  • The attachment directory is maintained by excel, mainly to maintain the attachment name and unique code and associated business code, and to update business records.
  • The uploaded attachments are packaged into zip format.
  • Create a directory on the ftp server to store the uploaded files.

Features

Use commons-fileupload-1.3.1.jarand commons-io-2.4.jartwo components, form to upload files.

Symptom

In the process of uploading files, the corresponding temp file needs to be generated on the disk file for the uploaded file. Some strange problems occurred during the process. The uploaded file has not been found on the storage disk of the temporary file. I struggled for a whole afternoon, but I can’t find it. What is the problem with the code I wrote? After debugging, it was found that the file was generated, but the name code of the generated file was smaller than the code in the memory. As a result, when the zip file was parsed, it was always prompted that the file could not be found.

Solution

  • 1. Download the commons-fileupload-1.3.1.jar source package
https://github.com/apache/commons-fileupload
  • 2. The debug process starts

Record

  • 1.getStoreLocation() returns an empty file
//存储在内存中文件,通过该方法获取的文件是空的
/**
     * Returns the {@link java.io.File} object for the {@code FileItem}'s
     * data's temporary location on the disk. Note that for
     * {@code FileItem}s that have their data stored in memory,
     * this method will return {@code null}. When handling large
     * files, you can use {@link java.io.File#renameTo(java.io.File)} to
     * move the file to new location without copying the data, if the
     * source and destination locations reside within the same logical
     * volume.
     *
     * @return The data file, or {@code null} if the data is stored in
     *         memory.
     */
    public File getStoreLocation() {
    
    
        if (dfos == null) {
    
    
            return null;
        }
        if (isInMemory()) {
    
    
            return null;
        }
        return dfos.getFile();
    }
  • 2. Stored in the file by default, the file size is 10kb.
    /**
     * The threshold above which uploads will be stored on disk.
     */
    private final int sizeThreshold;
    /**
     * The default threshold above which uploads will be stored on disk.
     */
    public static final int DEFAULT_SIZE_THRESHOLD = 10240;
    /**
     * The threshold above which uploads will be stored on disk.
     */
    private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
  • 3. Obtain the path of temporary files
    /**
     * Creates and returns a {@link java.io.File File} representing a uniquely
     * named temporary file in the configured repository path. The lifetime of
     * the file is tied to the lifetime of the {@code FileItem} instance;
     * the file will be deleted when the instance is garbage collected.
     * <p>
     * <b>Note: Subclasses that override this method must ensure that they return the
     * same File each time.</b>
     *
     * @return The {@link java.io.File File} to be used for temporary storage.
     */
    protected File getTempFile() {
    
    
        if (tempFile == null) {
    
    
            File tempDir = repository;
            if (tempDir == null) {
    
    
                tempDir = new File(System.getProperty("java.io.tmpdir"));
            }

            String tempFileName = format("upload_%s_%s.tmp", UID, getUniqueId());

            tempFile = new File(tempDir, tempFileName);
        }
        return tempFile;
    }
  • 2. Delete temporary files
    /**
     * Deletes the underlying storage for a file item, including deleting any
     * associated temporary disk file. Although this storage will be deleted
     * automatically when the {@code FileItem} instance is garbage
     * collected, this method can be used to ensure that this is done at an
     * earlier time, thus preserving system resources.
     */
    @Override
    public void delete() {
    
    
        cachedContent = null;
        File outputFile = getStoreLocation();
        if (outputFile != null && !isInMemory() && outputFile.exists()) {
    
    
            outputFile.delete();
        }
    }

Guess you like

Origin blog.csdn.net/qq125281823/article/details/107623159