Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. package com.example.demo;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. import javax.servlet.http.HttpServletRequest;
  13.  
  14. import org.apache.tomcat.util.http.fileupload.FileItemIterator;
  15. import org.apache.tomcat.util.http.fileupload.FileItemStream;
  16. import org.apache.tomcat.util.http.fileupload.FileUploadException;
  17. import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.http.HttpStatus;
  20. import org.springframework.http.ResponseEntity;
  21. import org.springframework.stereotype.Component;
  22. import org.springframework.web.bind.annotation.CrossOrigin;
  23. import org.springframework.web.bind.annotation.PostMapping;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import org.springframework.web.multipart.MultipartFile;;
  28.  
  29. @RestController
  30. @CrossOrigin
  31. @RequestMapping(value = "/getFiles")
  32.  
  33. @Component
  34. public class FileController {
  35. @Autowired
  36. FileService fileService;
  37.  
  38. @PostMapping("/postFile")
  39. public ResponseEntity<String> receiveFile(@RequestParam("file") MultipartFile file) throws IOException {
  40. HttpStatus status = HttpStatus.OK;
  41.  
  42. // CSVParser parser = CSVFormat.newFormat(',')
  43. // .parse(new InputStreamReader(new ByteArrayInputStream(file),
  44. // "UTF8"));
  45. // BufferedWriter writer = new BufferedWriter(new
  46. // FileWriter("C:\\Users\\Daniel Dimitrov\\Documents\\new.csv"));
  47. // //CSVPrinter printer = CSVFormat.newFormat(',').print(writer);
  48. //
  49. // CSVPrinter printer = new CSVPrinter(writer,
  50. // CSVFormat.newFormat(','));
  51. // for (CSVRecord record : parser) {
  52. // try {
  53. // printer.printRecord(record);
  54. // } catch (Exception e) {
  55. // throw new RuntimeException("Error at line " +
  56. // parser.getCurrentLineNumber(), e);
  57. // }
  58. // }
  59. return null;
  60.  
  61. }
  62.  
  63. @Autowired
  64. TestClass testClass;
  65.  
  66. @PostMapping("/postFileLarge")
  67. public ResponseEntity<String[][]> handleUploadLogs(HttpServletRequest request)
  68. throws IOException, FileUploadException {
  69.  
  70. ArrayList<String> linesResult = new ArrayList();
  71.  
  72. try (final InputStream is = getInputStream(request)) {
  73.  
  74. if (is != null) {
  75. String path = testClass.testSenderFile(is);
  76. File file = new File(path);
  77. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  78.  
  79. reader.lines().limit(21).forEach(line -> {
  80. linesResult.add(line);
  81. //System.out.println(line);
  82. });
  83. }
  84. }
  85.  
  86. String[][] result = new String[21][linesResult.get(0).length()];
  87. for (int j = 0; j < linesResult.size(); j++) {
  88. result[j] = linesResult.get(j).replace("\" ", "").split(",");
  89.  
  90. }
  91.  
  92. return new ResponseEntity(result, HttpStatus.OK);
  93. }
  94.  
  95. private InputStream getInputStream(final HttpServletRequest request) throws IOException, FileUploadException {
  96.  
  97. final ServletFileUpload upload = new ServletFileUpload();
  98.  
  99. final FileItemIterator iterator = upload.getItemIterator(request);
  100.  
  101. InputStream is = null;
  102.  
  103. while (iterator.hasNext()) {
  104. final FileItemStream item = iterator.next();
  105.  
  106. if (!item.isFormField()) {
  107.  
  108. is = item.openStream();
  109.  
  110. break;
  111. }
  112. }
  113.  
  114. return is;
  115.  
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement