過酷なKanakhara:
私は、クライアント側から取得していますInputStreamを使用して、サーバー側のファイルを作成しようとしました。
ファイルは元のファイルのと同じサイズで作成されます。しかし、それを開こうとすると、ファイルが破損していることを示しています。
クライアント・サーバ上のfileSharing.jsp共有ファイルにしようとしている(送信者)
HttpURLConnection httpURLConnection = null;
OutputStream os = null;
InputStream is = null;
try {
File fileObj = new File("D://test.pdf");
out.print("File Length " + fileObj.length() + " Name " + fileObj.getName());
URL url = new URL("http://localhost:2080/Receiver/fileupload?filename=test.pdf&filelength=" + fileObj.length());
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "multipart/mixed");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Cache-Control", "no-cache");
httpURLConnection.setRequestProperty("Content-Disposition", "form-data; name=\"Dummy File Description\"");
httpURLConnection.setChunkedStreamingMode(8192);
httpURLConnection.connect();
is = new BufferedInputStream(new FileInputStream(fileObj));
os = new BufferedOutputStream(httpURLConnection.getOutputStream());
byte[] buff = new byte[8192];
int len = 0;
while ((len = is.read(buff, 0, buff.length)) > 0) {
os.write(buff, 0, len);
os.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
os.close();
httpURLConnection.disconnect();
}
サーバ側のFileUploadControllerサーブレットファイルをダウンロードしようとしている(レシーバー)
InputStream is = null;
OutputStream os = null;
try {
is = request.getInputStream();
int total = 0;
int bytes = 0;
os = new BufferedOutputStream(new FileOutputStream(new File("D://files//dummy.pdf")));
byte[] buff = new byte[8192];
while (true) {
if ((bytes = is.read(new byte[8192])) == -1) {
System.out.println("File shared successfully");
System.out.println("Total " + total);
break;
}
total = total + bytes;
System.out.println("Length " + bytes);
os.write(buff, 0, bytes);
//os.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
os.close();
}
ヨアヒム・ザウアー:
読みループではFileUploadController
、あなたの新しい配列をインスタンス化し、それに読み込まれていますが、それを継続して使用するためにそれを保持しません。
byte[] buff = new byte[8192];
while(true){
if((bytes = is.read(new byte[8192])) == -1){
しかし、あなたが書く buff
ファイルに(正しい長さのファイルを持っていますので、0バイトのフルになりますが、それは、0ではなく、実際のデータの完全なのです)。
との最後の行を置き換え
if((bytes = is.read(buff)) == -1){