How to I extract form data from a POST request eg. $ curl -X POST -d asdf=blah http://localhost:8000/proxy/http://httpbin.org/post - and I need to extract asdf=blah.
The way I am currently doing it relies heavily on the data I have read being in a certain format (I am assuming the form data is always on the last line). Is there a better (and/or simpler) way to get the data which does not depend on the format of the data being read ?
(Note: the proxy deals with both GET and POST requests)
Here is code I have written :
public class ProxyThread3 extends Thread {
private Socket clientSocket = null;
private OutputStream clientOutputStream;
private static final int BUFFER_SIZE = 32768;
public ProxyThread3(Socket socket) {
super("ProxyThread");
this.clientSocket = socket;
}
public void run() {
try {
clientOutputStream = clientSocket.getOutputStream();
// Read request
InputStream clientInputStream = clientSocket.getInputStream();
byte[] b = new byte[8196];
int len = clientInputStream.read(b);
String urlToCall = "";
if (len > 0) {
String userData = new String(b, 0, len);
String[] userDataArray = userData.split("\n");
//Parse first line to get URL
String firstLine = userDataArray[0];
for (int i = 0; i < firstLine.length(); i++) {
if (firstLine.substring(i).startsWith("http://")) {
urlToCall = firstLine.substring(i).split(" ")[0];
break;
}
}
//get request type
String requestType = firstLine.split(" ")[0];
String userAgentHeader = "";
//get USER-AGENT Header and Accept Header
for (String data : userDataArray) {
if (data.startsWith("User-Agent")) {
userAgentHeader = data.split(":")[1].trim();
break;
}
}
switch (requestType) {
case "GET": {
sendGetRequest(urlToCall, userAgentHeader);
break;
}
case "POST": {
String postParams = null;
//Get Form Data
if (!userDataArray[userDataArray.length - 1].isEmpty()) {
postParams = userDataArray[userDataArray.length - 1];
}
sendPostRequest(urlToCall, userAgentHeader, postParams);
break;
}
}
} else {
clientInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendPostRequest(String urlToCall, String userAgentHeader, String postParams) throws IOException {
URL urlToWriteAndReadFrom = new URL(urlToCall);
HttpURLConnection httpURLConnection = (HttpURLConnection) urlToWriteAndReadFrom.openConnection();
httpURLConnection.setRequestMethod("POST");
// set User-Agent header
httpURLConnection.setRequestProperty("User-Agent", userAgentHeader);
httpURLConnection.setDoOutput(true);
OutputStream urlOutputStream = httpURLConnection.getOutputStream();
if (postParams != null) {
urlOutputStream.write(postParams.getBytes());
urlOutputStream.flush();
}
urlOutputStream.close();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
InputStream dataReader = httpURLConnection.getInputStream();
//begin send response to client
byte inputInBytes[] = new byte[BUFFER_SIZE];
assert dataReader != null;
int index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);
while (index != -1) {
clientOutputStream.write(inputInBytes, 0, index);
index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);
}
clientOutputStream.flush();
}
}
private void sendGetRequest(String urlToCall, String userAgentHeader) throws IOException {
URL urlToReadFrom = new URL(urlToCall);
HttpURLConnection httpURLConnection = (HttpURLConnection) urlToReadFrom.openConnection();
// set True since reading and getting input
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
// set User-Agent header
httpURLConnection.setRequestProperty("User-Agent", userAgentHeader);
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
InputStream dataReader = httpURLConnection.getInputStream();
//begin send response to client
byte inputInBytes[] = new byte[BUFFER_SIZE];
assert dataReader != null;
int index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);
while (index != -1) {
clientOutputStream.write(inputInBytes, 0, index);
index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);
}
clientOutputStream.flush();
}
}
}
PS. I am new to all of this so if there are any errors in my code, please do point them out
Here is a full example on how to create a Java HTTP server that handles POST and GET requests.
https://www.codeproject.com/Tips/1040097/Create-a-Simple-Web-Server-in-Java-HTTP-Server
That being shared, this is quite primitive, I'd recommend using any third party library or light-weight Java server like Grizzly or Jetty if not a server like Apache Tomcat utilizing J2EE Servlets which are made for this purpose.
Related
I am using the GitHub API to fetch files from a repository. I have the functionality implemented and it does work, I download the needed files but I found something strange, out of the 4 files I get, one is empty (no content inside) even though when I go the to repository and open it there it is clearly with content. The rest of the files have their content in when downloaded. Any idea why that happens?
Here is my code:
public int downloadFromGithub(String repo, String fileName) throws IOException {
URL url = new URL(repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", ****);
connection.setRequestProperty("Accept", ****);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK && fileName!=null)
{
return saveFile(connection, fileName);
}
else { return connection.getResponseCode();}
}
public void downloadMultipleFilesFromGithub(String repo,String directoryPath) throws IOException {
URL url = new URL(repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", *****);
connection.setRequestProperty("Accept", "***");
String response = getResponseBody(connection);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray array = jsonObj.getJSONArray("tree");
for (int i=0; i < array.length(); i++) {
System.out.println(array.getJSONObject(i).get("path"));
String path = array.getJSONObject(i).get("path").toString();
if(path.contains("Scripts")){
String fileName = path.replace(scriptsDirectoryReplace, "");
downloadFromGithub(scriptsRepositoryDirectory+fileName,fileName);
}
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public String getResponseBody(HttpURLConnection conn) {
BufferedReader br = null;
StringBuilder body = null;
String line = "";
try {
br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
body = new StringBuilder();
while ((line = br.readLine()) != null)
body.append(line);
return body.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int saveFile(HttpURLConnection connection, String fileName) throws IOException {
// opens input stream from the HTTP connection
String saveFilePath = fileSaveDirectory + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream writer = new FileOutputStream(saveFilePath);
InputStream reader = connection.getInputStream();
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
}
reader.close();
writer.close();
return connection.getResponseCode();
}
I'm trying to create a connection to some remote server, and get the following warning in my LogCat: "W/OkHttpClient: A connection to "some url" was leaked. Did you forget to close a response body?"
Now I don't understand why is it happening.
I use Apache IO commons to count number of bytes sent and received, and it's the only use if mine with OkHTTP.
Can you please look at my code snippet and tell me what's wrong with it?
This is the code where I try to create a connection:
public static JSONObject getConfiguration(Context context) throws HttpRequestException {
long bytes = 0;
long netUsage = HttpUtils.getCurrentNetworkUsage(context);
int statusCode = 0, count = 0;
NetworkRequest.Status netStatus = NetworkRequest.Status.FAILED;
JSONObject commonInformation;
HttpURLConnection connection = null;
CountingInputStream cis = null;
InputStreamReader isr = null;
BufferedReader br = null;
InputStream is = null;
CountingOutputStream cos = null;
// result
JSONObject receivedConfig = null;
try {
commonInformation = ConfigurationProcessor.getCommonInformation(context);
if (commonInformation == null) {
return null;
}
URL url = new URL(BuildConfig.SERVER_CONFIG_URL);
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "url = " + url.getPath());
}
connection = url.getProtocol().equals("https")? getHttpsConnection(url) : getHttpConnection(url);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Content-Encoding", "gzip");
byte[] gzipped = HttpUtils.gzip(commonInformation.toString());
cos = new CountingOutputStream(connection.getOutputStream());
cos.write(gzipped);
cos.flush();
statusCode = connection.getResponseCode();
switch (statusCode) {
case 200: {
// get the response
is = connection.getInputStream();
cis = new CountingInputStream(is);
isr = new InputStreamReader(cis);
br = new BufferedReader(isr);
StringBuilder builder = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
builder.append(output);
}
receivedConfig = new JSONObject(builder.toString());
netStatus = NetworkRequest.Status.SUCCEEDED;
break;
}
case 502: {
throw new HttpRequestException("Received 502 error (Bad Gateway)", NetworkRequest.Type.GET_REMOTE_CONFIGURATION);
}
}
// analytics about the request
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "generating analytics about the http request");
}
count = receivedConfig == null ? 0 : 1;
if (cis != null) {
bytes = cis.getByteCount();
}
if (cos != null) {
bytes += cos.getByteCount();
}
} catch (Exception | OutOfMemoryError ex) {
LogUtils.e(TAG, ex.getMessage());
ex.printStackTrace();
} finally {
closeStream(is);
closeStream(br);
closeStream(isr);
closeStream(cis);
closeStream(cos);
if (connection != null) connection.disconnect();
createNetworkRequest(context, statusCode, netUsage, netStatus, count, bytes, NetworkRequest.Type.GET_REMOTE_CONFIGURATION, BuildConfig.SERVER_CONFIG_URL);
}
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "Received configuration data");
}
return receivedConfig;
}
I need to upload image to the web server and it require the ImageContent to be in byte[] at the documentation it said base64Binary but i tried base64 encoded string and no use
that is my class :
private class background extends AsyncTask<byte[],Void,String> {
String url = "http://www.sample.com/_mobfiles/CLS_Account.asmx/UploadImage";
String charset = "UTF-8";
String param1 = "jpg";
#Override
protected String doInBackground(byte[]... params) {
try {
String query = String.format("ImageContent=%s&imageExtenstion=%s", params[0], URLEncoder.encode(param2, charset));
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
String contentType = connection.getHeaderField("Content-Type");
String charset = null;
for (String param : contentType.replace(" ", "").split(";")) {
if (param.startsWith("charset=")) {
charset = param.split("=", 2)[1];
break;
}
}
if (charset != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response, charset))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
}
else {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = response.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] arr = buffer.toByteArray();
String decoded = new String(arr, "UTF-8");
System.out.println(decoded);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
that return (java.io.FileNotFoundException)
and the Base64 Encoded return (java.net.ProtocolException: Unexpected status line: Object moved)
Here is the Doc :
HTTP GET
The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.
GET /_mobfiles/CLS_Account.asmx/UploadImage?ImageContent=base64Binary&imageExtenstion=string HTTP/1.1
Host: www.sample.com
and the response is like
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /_mobfiles/CLS_Account.asmx/UploadImage HTTP/1.1
Host: www.sample.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
ImageContent=base64Binary&imageExtenstion=string
and the response is like
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
Can you try this in your chrome console and tell the output.
xmlhttp=new XMLHttpRequest();
var url = '/_mobfiles/CLS_Account.asmx/UploadImage?';
var base64 ='R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='
xmlhttp.open("GET",url + 'ImageContent='+base64+'&imageExtenstion=gif',true);
xmlhttp.send();
console.log(xmlhttp.response)
Try this too. If this also fails you gotta check the server. Atleast provide server method responsible for handling this.
xmlhttp=new XMLHttpRequest();
var url = '/_mobfiles/CLS_Account.asmx/UploadImage?';
var base64 ='R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='
xmlhttp.open("POST",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('ImageContent="'+base64+'"&imageExtenstion="gif"');
console.log(xmlhttp)
Use a Get request and use
var base64 ='R0lGODlhDwAPAKECAAAAzMzM%2F%2F%2F%2F%2FwAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH%2BH09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw%3D%3D'
I am sure this should work. I am really sorry My bad I should haven't made such a stupid mistake/
I haven't found a way but to send the whole SOAP envelope
So from a background thread AsyncTask i called callSOAPWebService(String data) when executed
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap resizedBitmap = Bitmap.createScaledBitmap(thumbnail, 150, 150, false);
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100,stream);
byte[] byteArray = stream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
new ImageUpload().execute(encoded);
from the UI thread
and on background
#Override
protected String doInBackground(String... params) {
callSOAPWebService(params[0]);
return null;
}
and the callSOAPWebService is as follow
private boolean callSOAPWebService(String data) {
OutputStream out = null;
int respCode;
boolean isSuccess = false;
URL url;
HttpURLConnection httpURLConnection = null;
try {
url = new URL(GetData.NonOpDomain);
httpURLConnection = (HttpURLConnection) url.openConnection();
do {
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "keep-alive");
httpURLConnection.setRequestProperty("Content-Type", "text/xml");
httpURLConnection.setRequestProperty("SendChunked", "True");
httpURLConnection.setRequestProperty("UseCookieContainer", "True");
HttpURLConnection.setFollowRedirects(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(true);
httpURLConnection.setRequestProperty("Content-length", getReqData(data).length + "");
httpURLConnection.setReadTimeout(100 * 1000);
// httpURLConnection.setConnectTimeout(10 * 1000);
httpURLConnection.connect();
out = httpURLConnection.getOutputStream();
if (out != null) {
out.write(getReqData(data));
out.flush();
}
respCode = httpURLConnection.getResponseCode();
Log.e("respCode", ":" + respCode);
} while (respCode == -1);
// If it works fine
if (respCode == 200) {
try {
InputStream responce = httpURLConnection.getInputStream();
String str = convertStreamToString(responce);
System.out.println(".....data....." + str);
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(is, null);
parseXMLAndStoreIt(myparser);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
isSuccess = false;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out = null;
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
httpURLConnection = null;
}
}
return isSuccess;
}
and the helper method
public volatile boolean parsingComplete = true;
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("UploadImageResult")) {
uploadedImage = text;
uploadedImage = uploadedImage.replace("\"", "");
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public static String createSoapHeader() {
String soapHeader;
soapHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + ">";
return soapHeader;
}
public static byte[] getReqData(String data) {
StringBuilder requestData = new StringBuilder();
requestData.append(createSoapHeader());
requestData.append("<soap:Body>" + "<UploadImage" + " xmlns=\"http://example.org/\">" + "<ImageContent>").append(data).append("</ImageContent>\n").append("<imageExtenstion>jpg</imageExtenstion>").append("</UploadImage> </soap:Body> </soap:Envelope>");
Log.d("reqData: ", requestData.toString());
return requestData.toString().trim().getBytes();
}
private static String convertStreamToString(InputStream is)
throws UnsupportedEncodingException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Hope it will help some one in the future
I am trying to call my HttpServer with a POST and send a message in the body, on the server side I can see that it is called twice and I cannot figure out why.
Here is a part of the client code
String URL = "http://localhost:8081/" + path +"/service?session=" + sessionId;
connection = openConnection(URL, "POST");
OutputStream output = connection.getOutputStream();
output.write("Some Random body data".getBytes());
output.close();
stream = connection.getInputStream();
stream.close();
connection.disconnect();
On the server side i can see that the service is called two times. I figure it has to do something with my OutputStream and InputStream, but if i dont call the input stream it wont call the service any time.
EDIT!!!
Here is some more code
public class Server {
private static final int BASE_PORT = 8081;
public static void main(String[] args) {
try{
InetSocketAddress address = new InetSocketAddress(BASE_PORT);
HttpServer server = HttpServer.create(address, 0);
server.createContext("/", new PathDelegator());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on : " + BASE_PORT);
}catch(IOException e){
e.printStackTrace();
}
}
}
public class PathDelegator implements HttpHandler{
public void handle(HttpExchange exchange) throws IOException {
String URI = exchange.getRequestURI().toString();
if(URI.indexOf("/session") != -1){
//Call ServiceHandler
System.out.println("Call ServiceHandler");
serviceHandler(exchange, "some session key");
}
}
private void serviceHandler(HttpExchange exchange, String sessionId) throws IOException{
String requestMethod = exchange.getRequestMethod();
OutputStream responseBody = exchange.getResponseBody();
if(requestMethod.equalsIgnoreCase("POST")){
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
InputStream stream = exchange.getRequestBody();
int b = 0;
StringBuffer buffer = new StringBuffer();
while((b = stream.read()) != -1){
buffer.append((char)b);
}
System.out.println("body data: " + buffer.toString());
exchange.sendResponseHeaders(200, 0);
}else {
exchange.sendResponseHeaders(400, 0);
}
responseBody.close();
}
}
public class ClientTest {
#Test
public void shouldBeAbleToPostToService(){
try {
String SCORE_URL = "http://localhost:8081/service?session=" + sessionId;
connection = openConnection(URL, "POST");
OutputStream output = connection.getOutputStream();
output.write("Some body data".getBytes());
output.close();
stream = connection.getInputStream();
stream.close();
connection.disconnect();
fail("Not implemented yet!");
} catch (IOException e) {
e.printStackTrace();
}
}
private HttpURLConnection openConnection(String url, String method) throws IOException{
URL connectionURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection)connectionURL.openConnection();
connection.setRequestMethod(method);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
return connection;
}
}
Finallty i see that the System.out.println("body data: " + buffer.toString()); is outputted two times
Well, i finally figured out what was going on...
i had a method
public synchronized boolean addValue(int id, int value){
Integer previous = values.put(id, value);
return previous.intValue() != value;
}
problem was that the first time, the put would return a NULL value and as soon as i took care of this method the error does not occur no more.
My download scenario is: when i click on download link on jsp it calls signed applet method with file's id, from applet I call server side method by passing that id. I can get that file at server side but I want to return/pass that file back to my applet function.
My question is how to return back or pass downloaded file to my applet?
Or How can I set a file to response object at server side that can be useful at applet?
My Signed Applet :
private static void downloadEncryptedFile(String uuid) throws HttpException, IOException {
String uri = "http://localhost:8080/encryptFileDownload.works?uuid="+uuid;
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
client.executeMethod(postMethod);
postMethod.releaseConnection();
}
My Server side function:
#RequestMapping(value = "/encryptFileDownload/{uuid}.works", method = RequestMethod.POST)
public String downloadEncryptFile(#PathVariable("uuid") String uuid, HttpSession session, HttpServletResponse response) {
try {
if (StringUtils.isNotEmpty(uuid)) {
LOG.info("-----UUID----");
Node node = contentRetrieveService.getByNodeId(uuid);
Node resource = node.getNode("jcr:content");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + node.getName() + "\"");
InputStream in = resource.getProperty("jcr:data").getBinary().getStream();
ServletOutputStream outs = response.getOutputStream();
int i = 0;
while ((i = in.read()) != -1) {
outs.write(i);
}
outs.flush();
outs.close();
in.close();
LOG.info("File Downloaded");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I got the solution; I just wanted to pass a file id download it and return that file back to my applet, hence I have made changes in my code as:
My Applet:
try {
URL urlServlet = new URL("uri for your servlet");
URLConnection con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
// send data to the servlet
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(uuid);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String name = con.getHeaderField("filename");
File fi = new File(name);
int i = 0;
while ((i = inputFromServlet.read()) != -1) {
System.out.println(inputFromServlet.readLine());
}
inputFromServlet.close();
instr.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Server side Function just replace with this:
OutputStream outs = response.getOutputStream();
outputToApplet = new ObjectOutputStream(outs);
int i = 0;
while ((i = in.read()) != -1) {
outputToApplet.write(i);
}