Results 1 to 3 of 3
Thread: Http Input Stream read method
- 01-02-2011, 02:45 PM #1
Member
- Join Date
- Jan 2011
- Location
- Kolkata
- Posts
- 3
- Rep Power
- 0
Http Input Stream read method
I have a requirement to write a custom file server in java and send requests to it via a html page. Now I have tried it with first opening up a SerrverSocket at the 5000 port, and I have an html fom sending it a multipart request. I intend to capture the entire http reuest primarily and then use the apache commons library to separate out byte array from the multipart request. Via the read method I cant determine the EOF the stream. after the last byte is read the read method blocks till the html window is closed. can anyone let me any other strategy to know to handle this current requirement.(after extracting the byte array I need to send back a response to the html page).
please find below sample java code ----
ServerSocket serverSocket = new ServerSocket(5000);
String FOLDER_NAME = "uploaded_files";
while(true) {
try{
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//BufferedReader reader = new BufferedReader(new InputStreamReader(is));
DataInputStream reader = new DataInputStream(is);
String currentLine;
int cnt = 0;
boolean postRequest = false;
String dataBoundary = null;
String fileName = null;
String yourName = null;
while((currentLine = reader.readLine()) != null) {
if(currentLine.contains("POST")) {
postRequest = true;
System.out.println("POST REQ AS EXPECTED VERY NICE");
continue;
}
if(!postRequest) {
System.out.println("NO POST REQ THIS BREAKING FLOW");
break;
} else {
if(currentLine.contains("Content-Type: multipart/form-data; boundary=")) {
System.out.println("found a boundary value header");
dataBoundary = currentLine.substring((currentLine.indexOf("bounda ry=") + "boundary=".length()), (currentLine.length() -1));
System.out.println("boundary value = ".concat(dataBoundary));
continue;
}
if(dataBoundary != null && currentLine.contains(dataBoundary)) {
cnt++;
}
if(cnt == 1) {
//move 3 lines
if(currentLine.contains("Content-Disposition: form-data; name=\"yourName\"")){
reader.readLine();//skip a line
}
System.out.println("Your name = ".concat(yourName = reader.readLine()));
continue;
} else if(cnt == 2) {
if(currentLine.contains("Content-Disposition: form-data; name=\"sentFile\"; filename=\"")){
fileName = currentLine.substring(currentLine.indexOf("filenam e=") + "filename=".length() + 1, currentLine.length() - 1);
System.out.println("File Name = ".concat(fileName));
reader.readLine();//skip a line , this would depict a content type header
reader.readLine();//skip a line, this would indicate a blank line to mark the start of data.
continue;
} else {
// write the content to os
if(currentLine != null && !currentLine.contains(dataBoundary)) {
baos.write(currentLine.concat("\r").getBytes());
//baos.write(currentLine.getBytes());
}
}
} else if( cnt == 3) {
System.out.println(("cnt [" + cnt).concat( "], current line [").concat(currentLine).concat("]"));
break;
}
}
}
if(fileName == null ||yourName == null) {
System.out.println("FileServer.main() dont bother about this" );
} else {
//send a response back
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
pw.write(responseMessage(yourName, fileName));
pw.flush();
//close output stream
pw.close();
//handle the request bytearray.
FileOutputStream fos = new FileOutputStream(FOLDER_NAME + "/" + fileName);
//fos.write(baos.toByteArray(), 0, baos.toByteArray().length - 1);
fos.write(baos.toByteArray(), 0, baos.toByteArray().length - 1);
fos.close();
}
//close input stream
reader.close();
socket.close();
baos.close();
} catch(Exception ex) {
ex.printStackTrace();
}
please find the content of the html page ---
<form method="post" id="fileForm" name="fileForm" enctype="multipart/form-data" action="http://localhost:5000/dontcare.do" >
Enter your file<input type="file" name="custom_file" />
<br/>
Click to Submit<input type="submit" value="button" onclick="submitDoc();"/>
</form>
Thanks & Regards
MishraC
- 01-03-2011, 01:03 AM #2
why wouldn't you just use a servlet ?
- 01-03-2011, 02:36 AM #3
Member
- Join Date
- Jan 2011
- Location
- Kolkata
- Posts
- 3
- Rep Power
- 0
See I know a servlet deployed to a tomcat and apache commons file upload library is an alternative option, but its an option i cant use. i kinda have the following restrictions/ requirements.
1. I work with a BPM product, which unfortunately has a bug in its file uploading mechanism
2. I cant access the web.xml of the BPM suite, which has an unified portal web app to cater to all BPM projects, not in deployment environments atleast.
3. I can use java classes by "cataloging" them, in the BPM poject.
Similar Threads
-
Object Input Stream EOFException
By FlyNn in forum New To JavaReplies: 1Last Post: 12-18-2010, 12:33 PM -
Input stream error
By Johnny68 in forum New To JavaReplies: 10Last Post: 08-05-2010, 06:20 PM -
Stream closed on a ClassLoader input
By RaistlinMajeren in forum Advanced JavaReplies: 15Last Post: 06-03-2010, 07:18 AM -
standard input stream storing to a generic method?
By vendetta in forum New To JavaReplies: 3Last Post: 01-29-2010, 08:13 PM -
TCP/IP Client program halts at the read method of the stream
By PradeepBadiger in forum NetworkingReplies: 0Last Post: 03-26-2009, 03:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks