在 JAVA 中上传时出错“message[java.lang.IllegalArgumentException: im == null!]”
我正在尝试上传图像文件和 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文件本身。它得到上传。但它的大小也是零字节!
我哪里错了???
有什么建议吗!!!
I am trying to upload a image file and zip file. First i have started with image upload, it gave me message[java.lang.IllegalArgumentException: im == null!
Error. But, still it uploaded the image. Then i have added code to upload zip file. Now also i am getting same error. But, unlike last time, the image only getting uploaded and its size is 0 bytes.
I am using DWR to bring the data to server,
DWR Script:
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);
}
});
}
This the CODE i am trying.
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;
}
Update: Console Log
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!]
Final Update
I have tried to upload zip file alone itself by commenting the other parts. Its get upload. But its size also zero bytes!!!
Where I am going wrong????
Any suggesstions!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在上传字段中获取文件的二进制值。
dwr.util.getValue("uploadImage");
的值要么是文件的路径,要么是空(如果浏览器不允许您读取本地文件路径)。所以基本上你正在提交文本或什么也没有,但试图将其作为文件读取。我曾经在 DWR 应用程序中实现了上传文件,但我使用 iframe 来处理文件上传功能。最新的浏览器(FF3.6+、Safari4+、Chrome)确实支持使用 XHR 发送文件,但不要指望您的用户会使用它们。
您可以使用诸如 FileUploader 之类的库来为您处理这个问题,他们甚至有一个 Java 示例服务器端。它使用 XHR(如果可用)并依靠 iframe 解决方法。
You can not get the binary value of a file in an upload field. The value of
dwr.util.getValue("uploadImage");
is either the path of the file or empty if the browser doesn't let you read the local file path. So basically you are submitting text or nothing but are trying to read it as a file.I once implemented an upload file in a DWR application but I used an iframe to handle the file uploading functionality. Recent browsers (FF3.6+, Safari4+, Chrome) do support sending files with XHR but don't count on your users to be using one them.
You could use an library such as FileUploader to handle this for you, they even have a Java example for the server side. It uses XHR if available and falls back on an iframe workaround.