zipファイルをアップロードしようとしています。私のプロジェクトでは、クライアント側でDWRを使用し、サーバー側でJavaを使用しています。データをアップロードするためのDWRチュートリアルで見たように(Webサイトにはありません。dwr.rarバンドルで提供されています)、以下の行で入力を取得します。
var image = dwr.util.getValue('uploadImage');
var file = dwr.util.getValue('uploadFile');
var color = dwr.util.getValue('color');
dwr.util.getValue()は、任意の要素(この場合はファイルオブジェクト)の値を取得するためのユーティリティです。//チュートリアルで言及されています。
だから、私は以下のコードでそのユーティリティを使用してzipファイルを取得します。
Javascript:
function uploadZip(){
var file = dwr.util.getValue("uploadFile");
dwr.util.setValue("uploadFile", null);
DataUpload.uploadData(file, function(data){
if(data != null){
$("#zipURL").html("<p>Upload Completed!!!</p>");
$("#zipURL").append("Location: "+data.path2);
}
});
}
HTML:
<html>
<head>ZIP Uploader
</head>
<body>
<table>
<tr><td>Select File: </td><td><input type="file" id="uploadFile" /></td>
<tr><td><input type="button" value="Upload" onclick="uploadZip()" /></td></tr> </table>
<div id="result"><span id="imgURL"></span>
<span id="zipURL"></span></div>
</body>
</html>
Javaコードは次のとおりです。
public class DataUpload {
private static String DATA_STORE_LOC = "D:/BeenodData/Trials/";
public Path uploadData(InputStream file) throws IOException{//In the tutorial the
//parameters are in type of BufferedImage & String.
//They used it for image and text file respectively.
//In an another example(out of DWR site) they used InputStream for receiving
//image
try {
byte[] buffer = new byte[1024];
int c;
File f2 = new File(DATA_STORE_LOC+dat+".zip");
path.setPath2(DATA_STORE_LOC+dat+".zip");
FileOutputStream fos = new FileOutputStream(f2);
c = file.read();
System.out.println(c);
while ((c = file.read()) != -1) {
fos.write(c);
}
file.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return path;
}
このコードはエラーなしで実行されます。ただし、出力は空のzipファイルです。私は何か間違ったことをしていることを知っています。私はそれを見つけることができません。
実際、私はInputStreamとしてzipファイルを受け取っています。
Javaを使用してInputStream(zipファイル)をzip.fileに書き込むにはどうすればよいですか?
Javaメソッドパラメータを次のように設定するとどうなります
ZipFile file
か?私はそれを試しませんでしたが、それは私がまだそれについて学ぶための良いチュートリアルを探しているからです。
どんな提案やリンクももっと感謝するでしょう!!!!! 前もって感謝します!!!