Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.demo;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Scanner;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.tomcat.util.http.fileupload.FileItemIterator;
- import org.apache.tomcat.util.http.fileupload.FileItemStream;
- import org.apache.tomcat.util.http.fileupload.FileUploadException;
- import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Component;
- import org.springframework.web.bind.annotation.CrossOrigin;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;;
- @RestController
- @CrossOrigin
- @RequestMapping(value = "/getFiles")
- @Component
- public class FileController {
- @Autowired
- FileService fileService;
- @PostMapping("/postFile")
- public ResponseEntity<String> receiveFile(@RequestParam("file") MultipartFile file) throws IOException {
- HttpStatus status = HttpStatus.OK;
- // CSVParser parser = CSVFormat.newFormat(',')
- // .parse(new InputStreamReader(new ByteArrayInputStream(file),
- // "UTF8"));
- // BufferedWriter writer = new BufferedWriter(new
- // FileWriter("C:\\Users\\Daniel Dimitrov\\Documents\\new.csv"));
- // //CSVPrinter printer = CSVFormat.newFormat(',').print(writer);
- //
- // CSVPrinter printer = new CSVPrinter(writer,
- // CSVFormat.newFormat(','));
- // for (CSVRecord record : parser) {
- // try {
- // printer.printRecord(record);
- // } catch (Exception e) {
- // throw new RuntimeException("Error at line " +
- // parser.getCurrentLineNumber(), e);
- // }
- // }
- return null;
- }
- @Autowired
- TestClass testClass;
- @PostMapping("/postFileLarge")
- public ResponseEntity<String[][]> handleUploadLogs(HttpServletRequest request)
- throws IOException, FileUploadException {
- ArrayList<String> linesResult = new ArrayList();
- try (final InputStream is = getInputStream(request)) {
- if (is != null) {
- String path = testClass.testSenderFile(is);
- File file = new File(path);
- BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- reader.lines().limit(21).forEach(line -> {
- linesResult.add(line);
- //System.out.println(line);
- });
- }
- }
- String[][] result = new String[21][linesResult.get(0).length()];
- for (int j = 0; j < linesResult.size(); j++) {
- result[j] = linesResult.get(j).replace("\" ", "").split(",");
- }
- return new ResponseEntity(result, HttpStatus.OK);
- }
- private InputStream getInputStream(final HttpServletRequest request) throws IOException, FileUploadException {
- final ServletFileUpload upload = new ServletFileUpload();
- final FileItemIterator iterator = upload.getItemIterator(request);
- InputStream is = null;
- while (iterator.hasNext()) {
- final FileItemStream item = iterator.next();
- if (!item.isFormField()) {
- is = item.openStream();
- break;
- }
- }
- return is;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement