Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

over writing a file on server

siva prakash
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

how to over write a file on server through java program.

for my application i have to read and write file to server, i was able to read file from server using urlconnection & inputstream methods,

i am struggling how to overrite a file or create a new file on server ,

its urgent,

pls help me

andra kan
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found some related material. Hope this is useful for you.

The PUT type request is a means of uploading files to the server. While uploading is its original intent, I have not seen it used much. Instead, POST is generally used to upload files. The PUT handler has this syntax:

public void doPut(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{ //your code here}
The doPut() method is called by the server (via the service method) to handle a PUT request. Uploading files from a browser has always been difficult. The idea behind the PUT operation is to make uploading straightforward. It is supposed to allow a client to place a file on the server, just like sending a file by FTP. The javadoc for this method warns that when overriding this method, you should leave intact any content headers sent with the request (including Content-Length, Content-Type, Content-Transfer-Encoding, Content-Encoding, Content-Base, Content-Language, Content-Location, Content-MD5, and Content-Range). This method is rarely used, but it is powerful if you need it.

Listing 4.5 is a simplified HTML page that creates a file upload page that will direct the file contents to a servlet.

Listing 4.5 HTML Form Example
<form enctype="multipart/form-data" method="PUT"
<input type="file" size="20" name="FileToUpload"
value="Select File">
<input type="submit" name="UploadFile" value="Upload">
<input type="reset" value="Reset">
Listing 4.6 is a servlet that can accept an uploaded file.

Listing 4.6 Servlet That Handles a File Upload from the Client
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UploadServlet extends HttpServlet
static final String dir="C:/temp";

public void doPut(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
PrintWriter outHTML = res.getWriter();

int i;
InputStream input;
input = req.getInputStream();
BufferedInputStream in =
new BufferedInputStream(input);
BufferedReader reader =
new BufferedReader(
new InputStreamReader(in));
File outputFile =
new File("c:/temp/out.txt");
FileWriter out =
new FileWriter(outputFile);

while ((i = != -1)


catch (IOException e) {}
You need to account for the header and footer lines that the stream attaches to the actual file contents. The stream looks like this:

Content-Disposition: form-data; name="FileToUpload";
Content-Type: application/octet-stream; name="Candle. txt"

// ...
//actual file content here
// ...

Content-Disposition: form-data; name="UploadFile"

Therefore, you will need to search (indexOf) for the start of the actual file content by looking for the Content-Type and subsequent name parameters like so:

int headerEnd = line.indexOf("Content-Type: ");
headerEnd = line.indexOf("name=\"", headerEnd);
headerEnd = line.indexOf("\"", headerEnd + 7); //last quote
Likewise, you need to search the end of the file for the telltale Content-Disposition and preceding "------------" marker like so:

int footerStart =
line.lastIndexOf ("Content- Disposition: ");
footerStart = line.lastIndexOf ("---------", footerStart);
Lastly, you will grab the text between the two like so:

fileContent = line.substring(headerEnd, footerStart);
You can refer to RFC 1867 to learn more about uploading files through an HTML form ( This is all tedious, so you might just grab an open source ( or commercial Bean that uploads files such as uploadBean ( or jspSmartUpload (

Listing 4.6 worked when placed in the doPost() method (and the form method of Listing 4.5 is set to post), but did not work in the doPut() method using IE or Opera against Tomcat (version 4). I verified that the doPut() method is called as expected in the servlet. However, even after much tweaking, this file upload code failed when placed in the doPut method as shown previously. If you only change doPut to doPost it works?! Although I need to research this problem with Tomcat, you do need to understand that PUT is used to upload files to a Web server and that this is usually done by non-browser, client-side Web content development tools.
I agree. Here's the link:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic