UploadController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.neo.controller;
  2. import com.neo.config.Configurations;
  3. import com.neo.fastdfs.FastDFSClient;
  4. import com.neo.fastdfs.FastDFSFile;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. @Controller
  17. public class UploadController {
  18. private static Logger logger = LoggerFactory.getLogger(UploadController.class);
  19. @Autowired
  20. private Configurations configuration;
  21. @GetMapping("/")
  22. public String index() {
  23. return "upload";
  24. }
  25. @PostMapping("/upload") //new annotation since 4.3
  26. public String singleFileUpload(@RequestParam("file") MultipartFile file,
  27. RedirectAttributes redirectAttributes) {
  28. if (file.isEmpty()) {
  29. redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
  30. return "redirect:uploadStatus";
  31. }
  32. try {
  33. // Get the file and save it somewhere
  34. String path=saveFile(file);
  35. redirectAttributes.addFlashAttribute("message",
  36. "You successfully uploaded '" + file.getOriginalFilename() + "'");
  37. redirectAttributes.addFlashAttribute("path",
  38. "file path url '" + path + "'");
  39. } catch (Exception e) {
  40. logger.error("upload file failed",e);
  41. }
  42. return "redirect:/uploadStatus";
  43. }
  44. @GetMapping("/uploadStatus")
  45. public String uploadStatus() {
  46. return "uploadStatus";
  47. }
  48. /**
  49. * @param multipartFile
  50. * @return
  51. * @throws IOException
  52. */
  53. public String saveFile(MultipartFile multipartFile) throws IOException {
  54. String[] fileAbsolutePath={};
  55. String fileName=multipartFile.getOriginalFilename();
  56. String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
  57. byte[] file_buff = null;
  58. InputStream inputStream=multipartFile.getInputStream();
  59. if(inputStream!=null){
  60. int len1 = inputStream.available();
  61. file_buff = new byte[len1];
  62. inputStream.read(file_buff);
  63. }
  64. inputStream.close();
  65. FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
  66. try {
  67. fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs
  68. } catch (Exception e1) {
  69. e1.printStackTrace();
  70. }
  71. if (fileAbsolutePath==null) {
  72. System.out.println("upload file failed,please upload again!");
  73. }
  74. String path=configuration.getFdfsUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
  75. return path;
  76. }
  77. }