Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a servlet to read file send by a client application

Status
Not open for further replies.
Sep 5, 2006
37
0
0
US
Hi,

I am trying to write a servlet that will be called by some third party client and send a file. Inside the servlet I want to read that file and store it on a filesystem. Can someone guide me on how to handle this. How can I also check if there is really some data send by the client. For testing how can I write client application that calls this servlet and pass in a file i.e. text,binary etc. and in the servlet read and store.

Thanks
 
Would it not be easier just to use ftp?

Tim
 
Yes but my requirement is to have a servlet that some client will call to send the data in a file over, so trying to see how to achieve this. Here is what I have so far I have:

Code:
//client side I have this
FileInputStream fileInputStream = new FileInputStream( new File("exsistingFileName") );
   // open a URL connection to the Servlet 
   URL url = new URL(urlString);
   // Open a HTTP connection to the URL
   conn = (HttpURLConnection) url.openConnection();
   // Allow Inputs
   conn.setDoInput(true);
   // Allow Outputs
   conn.setDoOutput(true);
   // Don't use a cached copy.
   conn.setUseCaches(false);
   // Use a post method.
   conn.setRequestMethod("POST");
   conn.setRequestProperty("Connection", "Keep-Alive");  
   conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
   dos = new DataOutputStream( conn.getOutputStream() );
   dos.writeBytes(twoHyphens + boundary + lineEnd);
   dos.writeBytes("Content-Disposition: form-data; name=\"upload\";"
      + " filename=\"" + exsistingFileName +"\"" + lineEnd);
   dos.writeBytes(lineEnd);   

   // create a buffer of maximum size
   bytesAvailable = fileInputStream.available();
   bufferSize = Math.min(bytesAvailable, maxBufferSize);
   buffer = new byte[bufferSize];
   // read file and write it into form...
   bytesRead = fileInputStream.read(buffer, 0, bufferSize);
   while (bytesRead > 0)
   {
    dos.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
   }

   // send multipart form data necesssary after file data...
   dos.writeBytes(lineEnd);
   dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
   // close streams
   fileInputStream.close();
   dos.flush();
   dos.close();


//In the servlet I have this
PrintWriter out = response.getWriter();
      
      InputStream in = request.getInputStream();
      BufferedReader r = new BufferedReader(new InputStreamReader(in));
      StringBuffer buf = new StringBuffer();
      String line;
      while ((line = r.readLine())!=null)
      {
        buf.append(line);
      }
      String s = buf.toString();   
      out.println("SUCCESS");
      out.close();

But when I run this I don't see anything being passed over to the servlet. Any help is appreciated.

Thanks
 
In my case the files will be upload through a java application and there is no front end. How do I do this.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top