2015-04-17 43 views
0

我正在使用JCIFS SmbFileOutputStream写入将jpg(和mp4)文件上传到本地Windows网络上的共享。虽然这在大多数情况下运作良好,但我有时会发现产生的文件已损坏 - 例如,如果是JPG格式,也许只有照片的顶部可以清晰可见。SmbFileOutputStream写入创建损坏的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"; 

回答

0
sfos.close(); 

需要去之前

fileInputStream.close(); 

关闭输出关闭输入之前, 添加 'sfos.flush()' 不能伤害。