[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-10-15
5
Medium Priority
?
2,412 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 28

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 28

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 28

Accepted Solution

by:
rrz earned 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Suggested Courses

834 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