ピットレコードのファイルアップロード10kbステップ

概要

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

要求する

  • 添付ファイルカタログは、主に添付ファイル名と一意のコードおよび関連するビジネスコードを維持し、ビジネスレコードを更新するために、Excelによって維持されます。
  • アップロードされた添付ファイルはzip形式でパッケージ化されています。
  • アップロードされたファイルを保存するために、ftpサーバー上にディレクトリを作成します。

特徴

使用commons-fileupload-1.3.1.jarしてcommons-io-2.4.jar2つのコンポーネントが、ファイルをアップロードするために形成します。

症状

ファイルをアップロードする過程で、アップロードしたファイルのディスクファイルに対応する一時ファイルを生成する必要があります。処理中に奇妙な問題がいくつかあります。アップロードしたファイルが一時ファイルのストレージディスクに見つかりません。午後中ずっと苦労しましたが、見つかりませんでした。書いたコードの何が問題なのですか?デバッグ後、ファイルが生成されたことがわかりましたが、生成されたファイルの名前のエンコードがエンコードよりも小さかったその結果、zipファイルが解析されると、ファイルが見つからないというプロンプトが常に表示されました。

解決

  • 1.commons-fileupload-1.3.1.jarソースパッケージをダウンロードします
https://github.com/apache/commons-fileupload
  • 2.デバッグプロセスが開始されます

記録

  • 1.getStoreLocation()は空のファイルを返します
//存储在内存中文件,通过该方法获取的文件是空的
/**
     * 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.デフォルトでファイルに保存され、ファイルサイズは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.一時ファイルのパスを取得します
    /**
     * 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.一時ファイルを削除します
    /**
     * 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();
        }
    }

おすすめ

転載: blog.csdn.net/qq125281823/article/details/107623159