2008-09-03 7 views
4

サーブレットとApache Commons FileUploadを使用してファイルをセットディレクトリにアップロードするJavaコードがいくつかあります。文字データ(テキストファイルなど)はうまくいきますが、画像ファイルが文字化けしています。私はそれらを開くことができますが、イメージは見えるように見えません。ここに私のコードは次のとおりです。画像が文字化けするのはなぜですか?

サーブレット

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
    try { 
     String customerPath = "\\leetest\\"; 

     // Check that we have a file upload request 
     boolean isMultipart = ServletFileUpload.isMultipartContent(request); 

     if (isMultipart) { 
     // Create a new file upload handler 
     ServletFileUpload upload = new ServletFileUpload(); 

     // Parse the request 
     FileItemIterator iter = upload.getItemIterator(request); 
     while (iter.hasNext()) { 
      FileItemStream item = iter.next(); 
      String name = item.getFieldName(); 
      if (item.isFormField()) { 
      // Form field. Ignore for now 
      } else { 
      BufferedInputStream stream = new BufferedInputStream(item 
       .openStream()); 
      if (stream == null) { 
       LOGGER 
        .error("Something went wrong with fetching the stream for field " 
         + name); 
      } 

      byte[] bytes = StreamUtils.getBytes(stream); 
      FileManager.createFile(customerPath, item.getName(), bytes); 

      stream.close(); 
      } 
     } 
     } 
    } catch (Exception e) { 
     throw new UploadException("An error occured during upload: " 
      + e.getMessage()); 
    } 
} 

StreamUtils.getBytes(ストリーム)は、次のようになります。

public static byte[] getBytes(InputStream src, int buffsize) 
     throws IOException { 
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
    byte[] buff = new byte[buffsize]; 
    while (true) { 
     int nBytesRead = src.read(buff); 
     if (nBytesRead < 0) { 
     break; 
     } 
     byteStream.write(buff); 
    } 

    byte[] result = byteStream.toByteArray(); 
    byteStream.close(); 

    return result; 
} 

そして最後にFileManager.createFileは、次のようになります。

public static void createFile(String customerPath, String filename, 
     byte[] fileData) throws IOException { 
    customerPath = getFullPath(customerPath + filename); 
    File newFile = new File(customerPath); 
    if (!newFile.getParentFile().exists()) { 
     newFile.getParentFile().mkdirs(); 
    } 

    FileOutputStream outputStream = new FileOutputStream(newFile); 
    outputStream.write(fileData); 
    outputStream.close(); 
    } 

誰でも見つけることができ私は間違っているの?

乾杯、 リー

答えて

4

ことの一つは、(StreamUtils.getBytesからここでは、このブロックである):6行目で

1 while (true) { 
2 int nBytesRead = src.read(buff); 
3 if (nBytesRead < 0) { 
4  break; 
5 } 
6 byteStream.write(buff); 
7 } 

、それは問題で読み込まれるバイト数は、バッファ全体が書き込まれていないI。これが常にそうであると確信していません。これは、このような、より正確になります:

1 while (true) { 
2 int nBytesRead = src.read(buff); 
3 if (nBytesRead < 0) { 
4  break; 
5 } else { 
6  byteStream.write(buff, 0, nBytesRead); 
7 } 
8 } 

注ライン上の2つの追加パラメータとともに、5行目の「他の」(配列のインデックス開始位置とコピーする長さ)6.

私ができます画像のような大きなファイルの場合は、バッファがいっぱいになる前に戻ってくることを想像してください(もっと待っているかもしれません)。つまり、意図せずにバッファの末尾に残っていた古いデータを書き込むことになります。これはほとんどの場合EoFでほとんどの場合起こります。バッファが1バイトを超えると仮定しますが、EoFの余分なデータはおそらくあなたの破損の原因ではありません。それは望ましくないことです。

0

あなたは画像が文字化けを介して、またはあなたが途中でいくつかのパケットをドロップしていないことを来ていないことを確認しています。

0

私にはわからないどのような違い、それをしかし、メソッドシグネチャの不一致があるようです。助け

public static byte[] getBytes(InputStream src, int buffsize) 

希望:あなたが含まメソッドソースは2つの引数を持っていながら、

byte[] bytes = StreamUtils.getBytes(stream); 

:あなたのdoPost()メソッドで呼び出さgetBytes()方法は、引数を1つしか持っています。

0

元のファイルとアップロードされたファイルでチェックサムを実行して、すぐに差異があるかどうか確認できますか?

差分がある場合は、変更されていないファイルの正確な部分を特定することができます。

気になるものは、ストリームの始まりや終わり、エンディアンです。私は好きではない

1

私はちょうどcommons ioを使用したいと思います。次にIOUtilsを実行してください。コピー(InputStream、OutputStream);

その他の便利なユーティリティメソッドがたくさんあります。

関連する問題