Solved

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

Posted on 2008-10-15
5
2,335 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

770 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