Results 1 to 2 of 2
  1. #1
    pazvanti is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default File Upload from one Servlet to another

    Mart of a school project, I need to make a servlet that receives via POST an URL to another servlet, a file path and it will need to upload the file from the specified address.
    The file isn't stored on the client, but on the server that runs the servlet.

    This is my doGet method from the servlet that triggers the upload:
    Java Code:
    protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		PrintWriter out = response.getWriter();
    
    		String path = request.getParameter("filePath");
    		String hostAddress = request.getParameter("hostAddr");
    		String fileIDString = request.getParameter("fileID");
    
    		if (path == null) {
    			out.print("Error path empty");
    			return;
    		}
    		if (hostAddress == null) {
    			out.print("Host Address not specified");
    			return;
    		}
    		if (fileIDString == null) {
    			out.print("File ID not specified");
    			return;
    		}
    
    		path = path.trim();
    		hostAddress = hostAddress.trim();
    		fileIDString = fileIDString.trim();
    
    		if (path.length() == 0) {
    			out.print("Error path empty");
    			return;
    		}
    		if (hostAddress.length() == 0) {
    			out.print("Host Address not specified");
    			return;
    		}
    		if (fileIDString.length() == 0) {
    			out.print("File ID not specified");
    			return;
    		}
    
    		int fileID = Integer.parseInt(fileIDString);
    		if (fileID <= 0) {
    			out.print("FileID is not OK");
    			return;
    		}
    
    		File file = new File(path);
    		if (!file.exists()) {
    			out.print("File does not exist");
    			return;
    		}
    
    		uploadFile(hostAddress, path, fileID);
    
    		out.print("OK - " + hostAddress);
    		return;
    	}
    Nothing to fancy. The problem is that I don't know how to do the "uploadFile(hostAddress, path, fileID);" method. So far I have this:
    Java Code:
    @Asynchronous
    	private void uploadFile(String urlString, String exsistingFileName,
    			int fileID) {
    		HttpURLConnection conn;
    
    		System.out.println("Sending File");
    
    		if (urlString.endsWith("/")) {
    			urlString += "uploadFileFromInstance";
    		} else {
    			urlString += "/uploadFileFromInstance";
    		}
    
    	}
    Please note, that this sin't done via form on a website or anything like this. Any help is appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: File Upload from one Servlet to another

    So servlet A has to request a file from servlet B on another server?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. how to upload a pdf,txt, any other file in servlet?
    By nidhi125 in forum Advanced Java
    Replies: 0
    Last Post: 11-07-2011, 07:45 AM
  2. Ajax / servlet File upload
    By n3mesi83 in forum Java Servlet
    Replies: 0
    Last Post: 06-08-2010, 02:48 PM
  3. Replies: 6
    Last Post: 06-04-2009, 11:09 PM
  4. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 07:34 PM
  5. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •