![](/img/trans.png)
[英]java.lang.IllegalArgumentException: im == null! error
[英]Error While Uploading in 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文件。 它得到上傳。 但是它的大小也為零字節!!!
我要去哪里錯了????
任何建議!!!
您無法在上載字段中獲取文件的二進制值。 dwr.util.getValue("uploadImage");
是文件路徑,如果瀏覽器不允許您讀取本地文件路徑,則為空。 因此,基本上您只是在提交文本或不提交任何內容,而是嘗試將其作為文件讀取。
我曾經在DWR應用程序中實現了上傳文件,但是我使用iframe來處理文件上傳功能。 最近的瀏覽器(FF3.6 +,Safari4 +,Chrome)確實支持使用XHR發送文件,但不要指望用戶使用其中的一種。
您可以使用FileUploader之類的庫為您處理此問題,它們甚至在服務器端都有一個Java示例。 如果可用,它會使用XHR並依靠iframe解決方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.