aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes over writing a file on server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "over writing a file on server" Watch "over writing a file on server" New topic
Author

over writing a file on server

siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
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

thanks
siva
andra kan
Greenhorn

Joined: May 06, 2005
Posts: 4
Found some related material. Hope this is useful for you.

PUT
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
<html>
<body>
<form enctype="multipart/form-data" method="PUT"
action="localhost:8080/examples/servlet/UploadServlet">
<input type="file" size="20" name="FileToUpload"
value="Select File">
<input type="submit" name="UploadFile" value="Upload">
<input type="reset" value="Reset">
</form>
</body>
</html>
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.io.*;
import java.util.*;
import java.net.*;
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();
outHTML.println("done");

try
{
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 = reader.read()) != -1)
{
out.write(i);
}

out.close();
in.close();

}
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:

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

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

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

Upload
------------BB1rHqKAOHkiUoiFS3VI6v
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 (http://www.servlets.com/rfcs/rfc1867.html). This is all tedious, so you might just grab an open source (http://www.servlets.com/cos/index.html) or commercial Bean that uploads files such as uploadBean (http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html) or jspSmartUpload (http://www.jspsmart.com/).

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.
 
wood burning stoves
 
subject: over writing a file on server