计算大概时间上传文件

时间:2016-04-18 18:21:23

标签: java file-upload

但问题是疑问。我正在将图像从JAR上传到我的服务器。我确实有一个代码来计算爬上所有服务器的时间,如下所示:

long startTime = System.currentTimeMillis();
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("TIME ::: " + (float)(elapsedTime / 1000) + " seg");

现在我正在考虑大致上升时间。这张JAR拍摄的照片质量非常好,延迟时间也很长,我想做的只是作为用户的附加信息。我可以想出第一张图片,计算我上传该图片所花费的金额,以及该值乘以用户在所选文件夹中的图像总值。这似乎是有效的,但是如果某种程度上更准确,某种程度上集成了Java或者非常相似的东西,那么它就想来找你。

现在我以这种方式将图像上传到我的服务器:

public boolean uploadWithThumb(File image, String filename) {
    reconnect();

    try {
        _sftp.cd(Config.Config.IMAGES_FOLDER);
        _sftp.put(new FileInputStream(image), filename, ChannelSftp.OVERWRITE); //no overwrite (, ChannelSftp.OVERWRITE)

        OutputStream output = new ByteArrayOutputStream();
        //preview
        if (Config.Config.WATERMARK_FILE == "" || !(new File(Config.Config.WATERMARK_FILE)).exists()) {
            //no watermark
            Thumbnails.of(image)
                .size(Config.Config.PREVIEW_SIZE, Config.Config.PREVIEW_SIZE)
                .outputQuality(Config.Config.PREVIEW_QUALITY)
                .toOutputStream(output);
        } else {
            BufferedImage prev = Thumbnails.of(image)
                                           .size(Config.Config.PREVIEW_SIZE, Config.Config.PREVIEW_SIZE)
                                           .outputQuality(Config.Config.PREVIEW_QUALITY)
                                           .asBufferedImage();
            int wsize = Config.Config.PREVIEW_SIZE * 75 / 100;
            BufferedImage water = Thumbnails.of(Config.Config.WATERMARK_FILE)
                                            .size(wsize, wsize)
                                            .asBufferedImage();
            Thumbnails.of(prev)
                      .size(Config.Config.PREVIEW_SIZE, Config.Config.PREVIEW_SIZE)
                      .watermark(Positions.CENTER, water, 0.5f)
                      .outputFormat(Utils.getExtensionNoDot(image.getAbsolutePath()))
                      .toOutputStream(output);
        }

        InputStream decodedInput = new ByteArrayInputStream(((ByteArrayOutputStream) output).toByteArray());
        _sftp.cd(Config.Config.PREVIEW_FOLDER);
        _sftp.put(decodedInput, filename, ChannelSftp.OVERWRITE); //no overwrite (, ChannelSftp.OVERWRITE)

        //thumbs
        output = new ByteArrayOutputStream();
        Thumbnails.of(image)
                  .size(Config.Config.THUMBS_SIZE, Config.Config.THUMBS_SIZE)
                  .toOutputStream(output);
        decodedInput = new ByteArrayInputStream(((ByteArrayOutputStream) output).toByteArray());
        _sftp.cd(Config.Config.THUMBS_FOLDER);
        _sftp.put(decodedInput, filename, ChannelSftp.OVERWRITE); //no overwrite (, ChannelSftp.OVERWRITE)
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

1 个答案:

答案 0 :(得分:0)

获取上传所有文件的大致时间的最简单方法是上传一个文件并计算速度:

long firstImageSize = getImageSize(0); //get the size of the first image in bytes
long startTime = System.currentTimeMillis();
UploadFirstImage();  //Upload the first image
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
float meanSpeed = (float)firstImageSize/ (float)(elapsedTime / 1000);  //mean speed in bytes/second

获得平均上传速度后,您必须获得所有其他图像的总体大小。然后你可以简单地计算大概的流逝时间:

long allImagesSize = 0;
for(int i = 1; i<numImages; i++){
  allImagesSize += getImageSize(i);  //Calculate the size of all the images in bytes
}
System.out.println("Eplapsed time ::: " + (float)(allImagesSize / meanSpeed) + " seg");

如果您想实时更新时间估算,可以使用线程定期计算。

相关问题