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!!!
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.