Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache Camel - Unable to propagate JMS Header Properties between Request - Response 3 17
allswap challenge 6 134
print map entry 34 75
eclipse with Angularjs2.0 3 66
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 …
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.

749 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