Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.iti.agrimarket.view; /** * * @author Amr */ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller public class FileUploadController { // @RequestMapping(method = RequestMethod.GET, value = "/getimage1") public String provideUploadInfo(Model model) { File rootFolder = new File( "C:\\Users\\Amr\\Documents\\GP\\Agri_Market\\agrimarket_gp_iti\\AgriMarket_v2.2"); List<String> fileNames = Arrays.stream(rootFolder.listFiles()).map(f -> f.getName()) .collect(Collectors.toList()); model.addAttribute("files", Arrays.stream(rootFolder.listFiles()).sorted(Comparator.comparingLong(f -> -1 * f.lastModified())) .map(f -> f.getName()).collect(Collectors.toList())); return "uploadForm"; } // @RequestMapping(method = RequestMethod.POST, value = "/uploadimage1") public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (name.contains("/")) { redirectAttributes.addFlashAttribute("message", "Folder separators not allowed"); return "redirect:/"; } if (name.contains("/")) { redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed"); return "redirect:/"; } if (!file.isEmpty()) { try { BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name))); FileCopyUtils.copy(file.getInputStream(), stream); stream.close(); redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + name + "!"); } catch (Exception e) { redirectAttributes.addFlashAttribute("message", "You failed to upload " + name + " => " + e.getMessage()); } } else { redirectAttributes.addFlashAttribute("message", "You failed to upload " + name + " because the file was empty"); } return "redirect:/index"; } }