0 Replies - 1989 Views - Last Post: 13 December 2008 - 01:14 PM Rate Topic: -----

#1 vijaykumardahiya  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 30-November 08

How display a Image on Servlet from File Upload

Post icon  Posted 13 December 2008 - 01:14 PM

Dear Sir,
My requirement is I want to display a Image on Servlet from File Upload.
But It not display on servlet,Its appear a downloaded form and downloaded on disk when click on save button.Other thing is that It not a .jpg form.that mean It does not open by photo gallery.that message show file format not supported by Photo Gallary.
I also install I the Live HTTP headers 0.14 from Google in Firefox.
I run the weblogic on Firefox and run that progarm but same issue persist.
I also used Internet Browser,but problem are not solved.
I already download comman-file upload-1.2.1.jar and comman-io-1.4 in lib folder and set this on classpath.
I try to solve that problem from a long time But Issue are not resolved.
Can you help me Please..
Here my files:
Home.html:
<html>
<form method="post" action="tmp" enctype="multipart/form-data">
File
<input type="file" name="file"/>
<input type="submit"/>
</form>
</html>

UploadFile.java:
import java.io.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import javax.activation.DataSource;
public class UploadFile extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse res)
{
try{
FileUpload fup=new FileUpload();
boolean isMultipart = FileUpload.isMultipartContent(req);
// Create a new file upload handler
System.out.println(isMultipart);
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
InputStream in=item.getInputStream();
int len = req.getContentLength();
System.out.println("Running Starting");
System.out.println("*************");
System.out.println("Length is: "+len); 
System.out.println("+++++++++++");
byte [] dataBytes = new byte[len]; 
int index=in.read(dataBytes, 0, len);
System.out.println("********");
System.out.println("index"+index);
System.out.println("+++++++++");
// res.reset();
res.setContentType("image/jpg");
res.setHeader("Content-Disposition", "attachment; filename=\"image.jpg\"");
System.out.println("One image writting");
res.getOutputStream().write(dataBytes,0,len);
res.getOutputStream().flush(); 
System.out.println("Suceesfully send");
}
}
catch(Exception e){System.out.println(e);}
}
}

web.xml:
<web-app>
<servlet>
<servlet-name>one</servlet-name>
<servlet-class>UploadFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/tmp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>
</welcome-file-list>
</web-app>

Please Help me..

Is This A Good Question/Topic? 0
  • +

Page 1 of 1