dr1v3r3k

uploadFile

Sep 23rd, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. @Controller
  2. public class UploadFileController {
  3.     UploadFileService service;
  4.  
  5.     public UploadFileController(UploadFileService service) {
  6.         this.service = service;
  7.     }
  8.  
  9.     public static String uploadDirectory = System.getProperty("user.dir")+"/uploads";
  10.     @RequestMapping("/")
  11.     public String UploadPage(Model model) {
  12.         return "uploadview";
  13.     }
  14.     @RequestMapping("/upload")
  15.     public String upload(Model model,@RequestParam("files") MultipartFile[] files) {
  16.         StringBuilder fileNames = new StringBuilder();
  17.         for (MultipartFile file : files) {
  18.             Path fileNameAndPath = Paths.get(uploadDirectory, file.getName());
  19.             fileNames.append(file.getName()+" ");
  20.             try {
  21.                 service.saveFile(fileNameAndPath, file.getBytes());
  22.                 //Files.write(fileNameAndPath, file.getBytes());
  23.             } catch (IOException e) {
  24.                 e.printStackTrace();
  25.             }
  26.         }
  27.         model.addAttribute("msg", "Successfully uploaded files "+fileNames.toString());
  28.         return "uploadstatusview";
  29.     }
  30. }
  31. @Service
  32. public class UploadFileService {
  33.     MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");
  34.     MongoClient mongoClient = new MongoClient(uri);
  35.     MongoDatabase database = mongoClient.getDatabase("test");
  36.     MongoCollection collection = database.getCollection("schedule_uploadfile");
  37.  
  38.     UploadFileRepository uploadFileRepository;
  39.  
  40.     public UploadFileService(UploadFileRepository uploadFileRepository) {
  41.         this.uploadFileRepository = uploadFileRepository;
  42.     }
  43.  
  44.     public void saveFile(Path fileNameAndPath, byte[] bytes) throws IOException {
  45.         Reader reader = Files.newBufferedReader(Paths.get("C:\\Users\\Pepe\\Desktop\\kopiagrafiku.csv")); //dopóki tego nie zmienie
  46.                                                                                                //blad nie występuje i zapisuje do bazy
  47.         CSVParser csvParser = new CSVParser(reader, CSVFormat.newFormat(';'));
  48.  
  49.         for (CSVRecord record : csvParser) {
  50.             Document grafik = new Document("date", "23.09.2019");
  51.             grafik.append("rodzaj_rozkladu", "powszedni");
  52.             grafik.append("grafik_dzienny", new Document()
  53.                     .append("workNumber", record.get(0))
  54.                     .append("busLine", record.get(1))
  55.                     .append("startTime", record.get(2))
  56.                     .append("endTime", record.get(3)));
  57.             if (record.get(0).isEmpty())
  58.                 continue;
  59.  
  60.             collection.insertOne(grafik);
  61.         }
  62.     }
  63. }
  64.  
  65. widok
  66. <body>
  67. <form action="/upload" method="post" enctype="multipart/form-data">
  68.     <input type="file" name="files" multiple>
  69. <input type="date" id="date" name="date">
  70.     <input type="submit" value="Zapisz Grafik">
  71. </form>
  72. </body>
Add Comment
Please, Sign In to add comment