0

我正在尝试上传图像文件和 zip 文件。首先我开始上传图片,它给了我message[java.lang.IllegalArgumentException: im == null!错误。但是,它仍然上传了图像。然后我添加了代码来上传 zip 文件。现在我也遇到了同样的错误。但是,与上次不同的是,图像只是被上传并且它的大小是 0 字节。

我正在使用 DWR 将数据带到服务器,

DWR 脚本:

function uploadImage(){
var image = dwr.util.getValue("uploadImage");
var file = dwr.util.getValue("uploadFile");
dwr.util.setValue("uploadImage", null);
dwr.util.setValue("uploadFile", null);
DataUpload.uploadData(image, file, function(data){
    if(data != null){
        $("#imgURL").html("<p>Upload Completed!!!</p>");
        $("#imgURL").append("Location: "+data.path1);
        $("#zipURL").html("<p>Upload Completed!!!</p>");
        $("#zipURL").append("Location: "+data.path2);
    }
});

}

这是我正在尝试的代码。

public class DataUpload {
private static String DATA_STORE_LOC = "D:/Uploaded/Trials/";
public Path uploadData(InputStream image, InputStream file) throws IOException{
Path path = new Path();
BufferedImage img = ImageIO.read(image);
Date date = new Date();
DateFormat format = new SimpleDateFormat("ss");
String dat = format.format(date);
System.out.println(dat);
try {
    path.setPath1(DATA_STORE_LOC+dat+".jpg");
    System.out.println(DATA_STORE_LOC+dat+".jpg");
    ImageIO.write(img, "jpeg", new File(DATA_STORE_LOC+dat+".jpg"));
    System.out.println(true);
    byte[] buffer = new byte[1024];
    int len;
    File f2 = new File(DATA_STORE_LOC+dat+".zip");
    path.setPath2(DATA_STORE_LOC+dat+".zip");
    OutputStream out = new FileOutputStream(f2);
    while((len = file.read(buffer)) > 0){
            out.write(buffer, 0, len);
    }
    file.close();
    out.close();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}
return path;

}

更新:控制台日志

49 //System.out.println(dat);
D:/Uploaded/Trials/49.jpg //System.out.println(DATA_STORE_LOC+dat+".jpg");
745859 [18820833@qtp-7494106-7] WARN  org.directwebremoting.dwrp.BaseCallMarshaller  - --Erroring: batchId[1] message[java.lang.IllegalArgumentException: im == null!]

最终更新

我试图通过评论其他部分来单独上传 zip 文件。它得到上传。但它的大小也是零字节!!!

我哪里错了???

任何建议!!!

4

1 回答 1

0

您无法在上传字段中获取文件的二进制值。的值dwr.util.getValue("uploadImage");要么是文件的路径,要么如果浏览器不允许您读取本地文件路径,则为空。所以基本上你提交文本或什么都没有,但试图将它作为文件读取。

我曾经在 DWR 应用程序中实现了一个上传文件,但我使用 iframe 来处理文件上传功能。最近的浏览器(FF3.6+、Safari4+、Chrome)确实支持使用 XHR 发送文件,但不要指望您的用户使用它们。

您可以使用诸如FileUploader之类的库来为您处理这个问题,他们甚至有一个用于服务器端的 Java 示例。它使用 XHR(如果可用)并回退到 iframe 解决方法。

于 2010-11-11T14:23:07.870 回答