Code sample to upload files in File Store using Java

Code sample to upload files in File Store using Java

  1. import java.io.File;
  2. import java.util.List;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;

  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;

  7. import com.catalyst.advanced.CatalystAdvancedIOHandler;

  8. import org.apache.commons.fileupload.FileItem;
  9. import org.apache.commons.fileupload.FileItemFactory;
  10. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  11. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  12. import org.apache.commons.io.FileUtils;
  13. import org.json.simple.JSONObject;

  14. public class MultipartDataExample implements CatalystAdvancedIOHandler {
  15. private static final Logger LOGGER = Logger.getLogger(InvoiceProcessor.class.getName());
  16. static String POST = "POST";
  17. JSONObject responseData = new JSONObject();

  18. @Override
  19. @SuppressWarnings("unchecked")
  20. public void runner(HttpServletRequest request, HttpServletResponse response) throws Exception {
  21. try {
  22. String url = request.getRequestURI();
  23. String method = request.getMethod();

  24. if ((url.equals("/processFile")) && method.equals(POST)) {

  25. FileItemFactory factory = new DiskFileItemFactory();
  26. ServletFileUpload upload = new ServletFileUpload(factory);
  27. List<FileItem> items = upload.parseRequest(request);

  28. //The List Items will contain your multipart data. You can iterate the list to get your file object and then use it according to your logic.
  29. } else {
  30. LOGGER.log(Level.SEVERE, "Error. Invalid Request");
  31. responseData.put("error", "Request Endpoint not found");
  32. response.setStatus(404);
  33. }

  34. } catch (Exception e) {
  35. LOGGER.log(Level.SEVERE, "Exception in InvoiceProcessor", e);
  36. responseData.put("error", "Internal server error occurred. Please try again in some time.");
  37. response.getWriter().write(responseData.toString());
  38. response.setStatus(500);
  39. }
  40. }
  41. }
    • Related Articles

    • File upload code samples

      For all file operations like uploading, downloading files from the File Store we've created a sample repository that can be accessed here.
    • Attaching a file from File Store in an email

      As of now, we do not have the support for attaching files while sending email.  For now, we would suggest you to upload the file to the Catalyst Filestore, and create an URL for the same using the Catalyst functions and share the URL via mail to ...
    • File in Catalyst File Store; Get file name

      You can use the getFileDetails method available in the Node SDK in order to get the name of a file before downloading it. A sample code snippet for getting file name and other file details is as follows :      let filestore = app.filestore();     let ...
    • Need to access files from File Store for Web Client Hosting

      If your application is hosted in Catalyst, then you can use the download link of the file itself to render the same in your Web Client.  For Example : <img src=“/baas/v1/project/1611000000****/folder/1611000000012142/file/16110000000*****/download> ...
    • Catalyst File Store ,"message":"socket hang up","value":{"code":"ECONNRESET"}

      {"code":"api/request_failure","message":"socket hang up","value":{"code":"ECONNRESET"}}. The ECONNRESET issue might occur if you have not handled the error with a try catch block in your code and the “catalyst serve” command is running. You can check ...