Getting File from User using Java Servlet and Calling Spring Boot API to upload the data

Viewed 31

There is a scenario where I get files from Users using one app which has a servlet for uploading files. I need to get this file and make a call to another SpringBoot app's API to store the data finally in an S3 bucket.

The SpringBoot App works fine, I am able to call a dummy endpoint of the same from the Servlet App.

I am facing trouble with forming a request to call the SpringBoot App's API. Mainly how do I get the content from the servlet's request and set it to the body before making a call to the SpringBoot app's api?

Below are some code snippets:

Code:

public static String callOAuthObjectStoreDestination(String destinationName, String relativePath, HttpServletRequest request) {
    try {
        String accessToken = ConnectivityUtility.getAccessTokenWithClientCredentials();
    
        String urlString = destConfig.getProperty("URL") + relativePath;
        URL url = new URL(urlString);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
    
        LOGGER.info("Calling OAuth URL: {}", uri.toURL());
    
        String boundary = Long.toHexString(System.currentTimeMillis());
    
        HttpURLConnection urlConnection = (HttpURLConnection) uri.toURL().openConnection(proxy);
        urlConnection.setDoInput(true); // by default true
        urlConnection.setDoOutput(true);
        urlConnection.setInstanceFollowRedirects(false);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
        urlConnection.setRequestMethod("POST"); // by default GET
        urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        urlConnection.setRequestProperty("Content-Length", String.valueOf(request.getContentLengthLong()));
        urlConnection.setUseCaches(false);
// How do I get the content from Request and set it as the body for this urlConnection???

Servlet Snippet:

public class ImageUploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ConnectivityUtility.callOAuthObjectStoreDestination("ObjectStorePOC", "/uploadImage", req);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

SpringBoot App's API:

@PostMapping(value = "/uploadImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ObjectStoreAppResponse<FileInfo>> uploadFile(@RequestParam MultipartFile uploadFile) throws InternalServerException, BadRequestException {
    try {
        FileInfo file = fileStorageService.uploadFile(
                uploadFile.getBytes(),
                uploadFile.getOriginalFilename(),
                uploadFile.getContentType()
        );
        return ObjectStoreAppResponse.ok(file);
    } catch (IOException e) {
        throw new InternalServerException();
    }
}
0 Answers
Related