SmbFileOutputStream写入创建损坏的jpg文件

时间:2015-04-17 14:13:54

标签: java upload corrupt jcifs

我使用JCIFS SmbFileOutputStream写入将jpg(和mp4)文件上传到本地Windows网络上的共享。虽然这在大多数情况下运行良好,但我有时会发现生成的文件已损坏 - 例如如果它是一个jpg,也许只有照片的顶部会清晰/可见。

我在try / catch块中上传,但它没有抛出异常。有什么方法可以验证文件是否已正确上传?

try {
    if (debugging_on) {
        logger.info("UploadService.011 UploadFiles: uploading file:" + destFileName);
    }
    SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
    FileInputStream fileInputStream = new FileInputStream(new File(
            sSourceFilePath));
    byte[] buf = new byte[16 * 1024 * 1024];
    int len;
    while ((len = fileInputStream.read(buf)) > 0) {
        sfos.write(buf, 0, len);
    }
    fileInputStream.close();
    sfos.close();
    // Update the database to include the date/time of this upload
    millisStart = Calendar.getInstance().getTimeInMillis();
    sql = "UPDATE upload_history SET file_uploaded_date = "
            + millisStart + " WHERE filename = '" + filename + "'";
    db.execSQL(sql);

} catch (Exception e) {
    mNotifyBuilder.setContentText("Upload error - check folder permissions");
    mNotificationManager.notify(1, mNotifyBuilder.build());
    return "WriteFailure";

1 个答案:

答案 0 :(得分:0)

sfos.close(); 

需要先行

fileInputStream.close();

关闭输入前关闭输出, 添加'sfos.flush()'不会有害。