Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to develop doPut() Servlet

 
Mutua Kakinyi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2733
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64696
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see the entry in the JspFaq on file uploading.
 
Mutua Kakinyi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2733
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apache Commons FileUpload no good? Its web site has a pretty extensive user guide.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic