我试图上传一个 zip 文件。在我的项目中,我在客户端使用 DWR,在服务器端使用 Java。正如我在 DWR 上传数据的教程中看到的(它不在他们的网站上。他们通过 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 文件。我知道我做错了什么。我找不到那个。
实际上,我收到一个 zip 文件作为 InputStream。
我应该如何使用 java 将 InputStream(一个 zip 文件)写入一个 zip.file?
如果我将 java 方法参数设置为 会发生什么
ZipFile file
?我没有尝试过,但是因为,我仍在寻找一个很好的教程来了解它。
任何建议或链接都会更加感激!!!!!!提前致谢!!!