aspose file tools*
The moose likes Servlets and the fly likes How to develop doPut() Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to develop doPut() Servlet" Watch "How to develop doPut() Servlet" New topic
Author

How to develop doPut() Servlet

Mutua Kakinyi
Greenhorn

Joined: Apr 21, 2011
Posts: 7
Someone help me develop a servlet that can handle a doPut(). I have created a java class below.

import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class ReadWrite {

private PrintWriter outputStream;
private BufferedReader inputStream;
private String inputASCIIFile;
private String outFile;
ArrayList<String> configFileContents;

public ReadWrite(String inputASCIIFile) {
this.inputASCIIFile = inputASCIIFile;
}

public void getOutputASCIIFile() throws FileNotFoundException, IOException {
String param = "C:\\Input\\" + inputASCIIFile + ".WRT";
File inputFile = new File(param);
String outputFile = "http://localhost:8080/ReadASCII/Data/"+ inputASCIIFile+ ".DAT";
URL url = new URL(outputFile); // /Data
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("PUT");
con.connect();
OutputStream os = con.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(con.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));
int code = con.getResponseCode();
if (code >= 200) {
// put the data...
int i;
// read byte by byte until end of stream
while ((i = bis.read()) > 0) {
bos.write(i);
}
bis.close();
bos.close();
}
}
}

For this class, i have a JSP page below where I pass a number like 0011

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Information</title>
</head>
<body>
<h2>When ready, click 'Transfer'</h2>
<form method="PUT" action="processInputFile">
<label for="name">Enter Cluster: </label>
<input type="text" id="cluster" name="cluster" size="4" maxlength="4"/><br><br>
<input type="submit" value="Transfer"/>
<input type="reset" value="Clear"/>
</form>
</body>
</html>

Now I need a servlet to take this number, which is filename being processed by java class. Finally, I need to upload this file at the servlet level.

Some help me and provide with some codes.

Thank you in advance.

Mutua
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
HTML forms can use GET or POST, but not PUT. What are you trying to accomplish by using PUT?

If you need to handle file uploads in a servlet, check out the Apache Commons FileUpload library; it makes that real easy.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Also see the entry in the JspFaq on file uploading.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mutua Kakinyi
Greenhorn

Joined: Apr 21, 2011
Posts: 7
Tim Moores wrote:HTML forms can use GET or POST, but not PUT. What are you trying to accomplish by using PUT?

If you need to handle file uploads in a servlet, check out the Apache Commons FileUpload library; it makes that real easy.


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Information</title>
</head>
<body>
<h2>When ready, click 'Transfer'</h2>
<form method="POST" action="processInputFile">
<label for="name">Enter Cluster: </label>
<input type="text" id="cluster" name="cluster" size="4" maxlength="4"/><br><br>
<input type="submit" value="Transfer"/>
<input type="reset" value="Clear"/>
</form>
</body>
</html>

I have correted to <form method="POST" action="processInputFile">

My intention is to upload a data (ASCII) file. I hae read many approaches but all seem complicated to understand. Any easier way?

Mutua
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Apache Commons FileUpload no good? Its web site has a pretty extensive user guide.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to develop doPut() Servlet