File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file uploading using apache

 
jagdish waran
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jagdish waran
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic