aspose file tools*
The moose likes Servlets and the fly likes file uploading  using apache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "file uploading  using apache" Watch "file uploading  using apache" New topic
Author

file uploading using apache

jagdish waran
Greenhorn

Joined: Mar 31, 2008
Posts: 6
Dear everyone,
Iam trying to upload files to server using servlets. iam using apache fileupload to upload the files . But iam facing problem when using List in the program to get the list of uploaded files.

can aybody help me.

below is the code

import java.util.*;
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class testapc extends HttpServlet
{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
try {
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
List items=upload.parseRequest(request);

}
catch(Exception ee)
{
out.println("ERROR IS :"+ee.getMessage());
}
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
Welcome to JavaRanch.

What problem is it you're facing? What are you expecting, and what is actually happening?
[ April 02, 2008: Message edited by: Ulf Dittmer ]
jagdish waran
Greenhorn

Joined: Mar 31, 2008
Posts: 6
Thank You for the consideration, i have sorted out the problem now i can upload files to server.

Now can you help me by giving any links to learn "HOW TO DOWNLOAD FILES FROM THE SERVER AND DISPLAY ON THE CLIENT MACHINE" or please give me some information on how to do this.

Thank you,
Jagdish K
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
What do you mean by "download"? The easiest way is to put a link to the files on a web page, and users can just click on it. If you're talking about something else, post the details of what you're trying to do.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

jagdish,
Please don't shout.


How you serve up files depends on where they are.
We have an example application in our CodeBarn that streams image files that are stored under the WEB-INF directory to the browser.

If the files are located within your application's directory structure but not under WEB-INF, a simple hyperlink should be sufficient.
If you're storing the files outside of the application's directory structure, you would want to do something similar to what's done in our codebarn example but use java.io.File to read the file off the server's disk instead of using context.getResourceAsStream.

You can find that example here:
http://faq.javaranch.com/java/CodeBarnSimpleStream


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
jagdish waran
Greenhorn

Joined: Mar 31, 2008
Posts: 6
Dear Mr BenSouther,

Iam new to java programming. Thanks for your courtesy.

I had reffered your link. Thank you. My problem is positioning the images at the right places in the html. In the example given by you the images has been stored under WEB-INF of the project. In my module the images are storerd in the root directory of the Tomcat server ie. in local system: C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.14\images\one.jpg
I want to access the image over the network and display it in appropriate table cell of my html when the client access the page.

What should i do to get the relative path of the above mentioned file and display the image.

Thank you,
jagdish K
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

That's not really a good place to keep files.

Java Servlet containers support the concept of web applications.
Each application has its own directory structure.
In Tomcat, by default these application directories go under tomcat/webapps.
If you keep your images in the application to which they belong, all you'll need to do to view them in a web pages is create a plain old <img ../> tag with a relative link to the images.
See: http://faq.javaranch.com/java/ResourceUrls to learn how to create proper context relative links.

If you must store your images outside of the web application (and I still wouldn't store them inside the Tomcat directory structure), you'll need to write a servlet, similar to the one in SimpleStream, that reads the file from disk (using java.io.FileReader) and streams it to the client.
This is, of course, much more work than linking to a file inside the application's directory structure.
jagdish waran
Greenhorn

Joined: Mar 31, 2008
Posts: 6
[ UD: Please start a new thread for completely unrelated questions. We like to keep each discussion thread focused on a single topic. What's more, you've already posted that question in a different forum. ]
[ April 08, 2008: Message edited by: Ulf Dittmer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file uploading using apache