使用commons-fileupload上传大文件的速度有多快

时间:2018-05-08 09:14:25

标签: java apache-commons-fileupload

我是Java的初学者,我使用Apache的commons-fileupload和conmmons-io来上传大文件。使用此代码我花了20秒钟复制40m的文件大小。这是正常的吗?我找到了很多没有答案的地方,我会感激任何帮助,谢谢。 这是复制官方代码的一个例子:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.io.FileUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet2 extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletFileUpload upload = new ServletFileUpload();
    try {
        FileItemIterator iter = upload.getItemIterator(req);
        System.out.println("start....");
        long startTime = System.currentTimeMillis();
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String name = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {

            } else {
                String value = item.getName();
                int start = value.lastIndexOf("\\");
                String fileName = value.substring(start + 1);
                System.out.println(fileName);
                FileUtils.copyInputStreamToFile(stream, new File("C:/dest.exe"));
                long endTime = System.currentTimeMillis();
                System.out.println("total seconds:" + (endTime - startTime) / 1000);
            }
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
}
}

0 个答案:

没有答案