Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite files from multiple rest requests
    primarykey
    data
    text
    <p>I have a rest service written to receive a file and save it.</p> <p>The problem is that when I receive more than 2 requests, the files are not written only the last request is taken into consideration and written.</p> <p>Here is my code:</p> <pre><code>@POST @RequestMapping(value = "/media/{mediaName}/{mediaType}") @Produces(MediaType.APPLICATION_OCTET_STREAM) @ResponseBody public String updateResourceLocally(@FormDataParam("rawData") InputStream rawData, @PathVariable("mediaName") String mediaName, @PathVariable("mediaType") String mediaType) { logger.info("Entering updateResourceLocally for " + jobId + "; for media type: " + mediaType); final String storeDir = "/tmp/test/" + mediaName + ("/"); final String finalExtension = mediaType; final InputStream finalRawData = rawData; // new Thread(new Runnable() { // public void run() { // writeToFile(finalRawData, storeDir, finalExtension); // } // }).start(); writeToFile(finalRawData, storeDir, finalExtension); // int poolSize = 100; // ExecutorService executor = Executors.newFixedThreadPool(poolSize); // executor.execute(new Runnable() { // @Override // public void run() { // writeToFile(rawData, storeDir, finalExtension); // } // }); logger.info("File uploaded to : " + storeDir); return "Success 200"; } </code></pre> <p>I tried to put the writeToFile into threads, but still no success. Here is what writeToFile does</p> <pre><code>public synchronized void writeToFile(InputStream rawData, String uploadedFileLocation, String extension) { StringBuilder finalFileName = null; String currentIncrement = ""; String fileName = "raw"; try { File file = new File(uploadedFileLocation); if (!file.exists()) { file.mkdirs(); } while (true) { finalFileName = new StringBuilder(fileName); if (!currentIncrement.equals("")) { finalFileName.append("_").append(currentIncrement).append(extension); } File f = new File(uploadedFileLocation + finalFileName); if (f.exists()) { if (currentIncrement.equals("")) { currentIncrement = "1"; } else { currentIncrement = (Integer.parseInt(currentIncrement) + 1) + ""; } } else { break; } } int read = 0; byte[] bytes = new byte[1024]; OutputStream out = new FileOutputStream(new File(uploadedFileLocation + finalFileName)); while ((read = rawData.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } </code></pre> <p>The writeToFile creates a folder and writes a file, if the file already exists, it appends 1 and then increments the 1 accordingly and writes the file, so I would get raw.zip, raw-1.zip, etc.</p> <p>I think the inputstream bytes are being lost, am I correct in my assumption?</p> <p>NOTE: I do not have a UI client, I am using Poster a Firefox extension.</p> <p>Update: What I am trying to achieve here is very simple</p> <ol> <li>I receive number of requests with files attached</li> <li>I need to save them. If the mediaName and mediaType are the same, then I need to append something to the filename and save it in the same location</li> <li>If they are different I do not have a problem</li> </ol> <p>The problem I am facing with the current code is that, when I post multiple time to the same URL, I have file-names created according to what I want, but the file content is not right, they vary depending on when the request came in and only the last POST's request is written properly.</p> <p>Eg. I have a zip file of size 250MB, when I post 5 time, the 1st four will have random sizes and the 5th will have the complete 250MB, but the previous four should also have the same content.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload