FileHelper.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package cn.seecoder.ai.util;
  2. import org.springframework.web.multipart.MultipartFile;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. /**
  9. * File dealing function kit
  10. * Created by lucas on 2016/10/17.
  11. */
  12. public class FileHelper {
  13. //Get the file with postfix
  14. public static String getFile(String path) {
  15. Path p = Paths.get(path);
  16. try {
  17. return p.getFileName().toString();
  18. } catch (Exception e) {
  19. return null;
  20. }
  21. }
  22. //Get the file without postfix
  23. public static String getFileName(String path) {
  24. try {
  25. return getFile(path).split("\\.")[0];
  26. } catch (Exception e) {
  27. //null value or Array out of Index.
  28. return null;
  29. }
  30. }
  31. public static String getFileType(String path) {
  32. try {
  33. String[] strs = getFile(path).split("\\.");
  34. return strs[strs.length - 1];
  35. } catch (Exception e) {
  36. return null;
  37. }
  38. }
  39. public static File multipartToFile(MultipartFile file) throws IOException {
  40. File convFile = new File(file.getOriginalFilename());
  41. convFile.createNewFile();
  42. FileOutputStream fos = new FileOutputStream(convFile);
  43. fos.write(file.getBytes());
  44. fos.close();
  45. return convFile;
  46. }
  47. /*public static void main(String[] args) {
  48. System.out.println(getFileType("/Users/lucas/Projects/Data/pubsaf1509/tx_in.csv"));
  49. }*/
  50. }