Upload elements are part of a multipart form, and unfortunately the HttpServletRequest cannot handle those. I suggest you use the FileUpload package to handle multipart requests:
http://jakarta.apache.org/commons/fileupload/using.html
When you do decide to use this, be aware of the dependencies needed for FileUpload to work:
http://jakarta.apache.org/commons/fileupload/dependencies.html
(you only need commons-io)
try this., this will upload image
InputStream ContentFileInput = null;
try {
String ContentFileName = file.getFileName();
ContentFileInput = file.getInputStream();
boolean isMultipart = FileUpload.isMultipartContent(request);
String uploadDir = servlet.getServletContext().getRealPath(
"/resources")
+ "/";
System.out.println(uploadDir);
uploadDir =uploadDir.substring(0,uploadDir.indexOf("resources")-1);
uploadDir = uploadDir +"\\upload\\";
System.out.println(uploadDir);
uploadDir = uploadDir + filePath;
InputStream mouseStream = file.getInputStream();
OutputStream mousebos = new FileOutputStream(uploadDir
+ ContentFileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = mouseStream.read(buffer, 0, 8192)) != -1) {
mousebos.write(buffer, 0, bytesRead);
}
String mouseLocation = "The file has been written to \""
+ dirPath.getAbsolutePath() + file.getFileName() + "\"";
System.out.println(mouseLocation);
mousebos.close();
} catch (Exception e) {
System.out.println(e);
> Can we use the FileUpload package for the same purpose? Sorry if it is a stupid question but i don't have much idea about j2ee.
Somewhere in your struts code you handle the request right? So that is where you would use FileUpload in stead of the regular HttpServletRequest object you get.
> It is struts framework. It has got action classes,
> forms and jsps. I need to upload just the image and
> then display it on the screen.
>
> Can we use the FileUpload package for the same
> purpose? Sorry if it is a stupid question but i don't
> have much idea about j2ee.