Solved

How to upload a file to remote server webapps virtual directory?

Posted on 2008-10-15
5
2,364 Views
Last Modified: 2013-11-24
Hi

I need to upload file to a remote server under webapps's virtual directory.
but, I do not know how to use the following code:
appPath.substring(0,appPath.indexOf("webapps")) + "temp");

and what are factory.setRepositoryPath and getRepositoryPath(); ?



import java.sql.*;
import java.io.*;
import java.io.File;
import java.net.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.FileItem;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class uploadfile extends HttpServlet {
 
private String uploadPath = "C:\\upload\\";    
private String tempPath = "C:\\upload\\tmp\\";     
File tempPathFile;
    
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try
{
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(50000);
factory.setRepository(tempPathFile);
//factory.setRepositoryPath(appPath.substring(0,appPath.indexOf("webapps")) + "temp");
//String tempAPath = factory.getRepositoryPath();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(50000);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
}

Open in new window

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 22721572
0
 

Author Comment

by:techques
ID: 22727573
Hi

I read your code and tried it.

File file = new File(application.getRealPath("/fileUploads/" + fileName));

I use servlet, either application.getRealPath or ServletContext.getRealPath said "cannot be resolved"  when I use eclipse.

0
 
LVL 27

Expert Comment

by:rrz
ID: 22727878
A JSP has an implicit object "application" available to it. If you are trying my code in a servlet,  then use
ServletContext application = getServletContext();
File file = new File(application.getRealPath("/fileUploads/"  + fileName));
This assumes that you have created a "fileUploads" directory in the root directory of your web app.
0
 

Author Comment

by:techques
ID: 22731129
I need to create a directory under webapps/test/
which test is the application folder
http://localhost:8080/test/
I want to create http://localhost:8080/test/upload/abc/

private String uploadPath = "/upload/abc/", filepath;
filepath = request.getContextPath() + uploadPath;
File tempPathFile = new File(filepath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}

The code does not create any folder.



request.getContextPath()

/upload/abc/

if i use getServletContext().getRealPath(uploadPath);

C:\\Tomcat 6.0\\webapps\\test\\upload\\abc
0
 
LVL 27

Accepted Solution

by:
rrz earned 100 total points
ID: 22732079
Try something like thi.
ServletContext application = getServletContext();
File dir = new File(application.getRealPath("/abc"));
if(dir.mkdir()){
         System.out.print("abc directory was created");
}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question