Let me see if I can explain this. The front-end provides me with a spreadsheet that I need to pass to my central server via an api call to do the processing. I use the following code to pull out the spreadsheet and create my work book,
Part spreadsheet = request.getPart(SPREADSHEET);
Workbook workbook = WorkbookFactory.create(spreadsheet.getInputStream());
Where 'request' is an incoming HttpServletRequest object. I know that it works and I can manipulate the spreadsheet, but I need to pass it along to my other server to do the processing and I can't figure out how to do that. Here is what I have so far.
@Path("/uploadSpreadsheet")
@POST
public String uploadSpreadsheet(@Context final HttpServletRequest request,@Context final HttpHeaders httpHeaders) throws IOException, ServletException, InvalidFormatException, JSONException {
return uploadUtil(request, "rest/memberService/uploadSpreadsheet");
}
Here is the util that I can't get right.
private String uploadUtil(HttpServletRequest request, String serviceUrl) throws MalformedURLException, ProtocolException, IOException, ServletException {
String baseUrl = "http://localhost:8084/centralservices/";
String urlString = baseUrl.concat(serviceUrl);
URL url = new URL(urlString);
String boundary = "===" + System.currentTimeMillis() + "===";
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpCon.setRequestProperty("Key", "SOMEKEYHERE");
httpCon.setRequestProperty("clientAddress", request.getRemoteAddr());
//I know this is wrong but I'm not sure what goes here:
httpCon.setRequestProperty("file", request.getPart(SPREADSHEET));
int responseCode;
StringBuilder resp = new StringBuilder();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(httpCon.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
resp.append(inputLine);
}
} finally {
if (in != null) {
in.close();
}
httpCon.disconnect();
}
return resp.toString();
}
Hopefully that makes sense. I mean all I really need to do is transfer the request identically(with all the formparts and everything) to a different url. Once it gets there I have no issue processing it. Let me know if I am unclear on something. I'm pretty new to this stuff. We already have a rest utility that I've always used for everything else, but it doesn't work for this scenario so I need to create a new one.