[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2423
  • Last Modified:

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

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
techques
Asked:
techques
  • 3
  • 2
1 Solution
 
rrzCommented:
0
 
techquesAuthor Commented:
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
 
rrzCommented:
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
 
techquesAuthor Commented:
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
 
rrzCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now