Advertisement
psi_mmobile

Untitled

Mar 4th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1.     private String uploadFile(UploadedFile doc) {
  2.         UploadedFile myDoc = doc;
  3.         String path = null;
  4.         if (myDoc != null) {
  5.             String fileName = myDoc.getFilename();
  6.             String uploadedFileName = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()) + "_" + fileName;
  7.             path = FILE_PATH + uploadedFileName;
  8.  
  9.             System.out.println(path + " PATH + FILENAME ");
  10.             InputStream inputStream = null;
  11.             File file;
  12.             try {
  13.                 URL pathURL = new URL(path);
  14.                 try {
  15.                     file = new File(pathURL.toURI());
  16.                 } catch (URISyntaxException e) {
  17.                     file = new File(pathURL.getPath());
  18.                 }
  19.                 FileOutputStream out = new FileOutputStream(file);
  20.                 inputStream = myDoc.getInputStream();
  21.                 byte[] buffer = new byte[8192];
  22.                 int bytesRead = 0;
  23.                 while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
  24.                     out.write(buffer, 0, bytesRead);
  25.                 }
  26.                 out.flush();
  27.                 out.close();
  28.             } catch (Exception ex) {
  29.                 log.error(ex.getMessage(), ex);
  30.             } finally {
  31.                 try {
  32.                     if (null != inputStream)
  33.                         inputStream.close();
  34.                 } catch (IOException ioex) {
  35.                     log.error(ioex.getMessage(), ioex);
  36.                 }
  37.             }
  38.         }
  39.         return path;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement