这是因为访问的文件地址是文件夹而不是,具体的文件,把路径改为具体的文件就可以了。

 参考文章:https://blog.csdn.net/w405722907/article/details/78610485

 

  1. /**
  2. * 查看某个文件夹下所有的文件 并删除文件
  3. */
  4. @RestController
  5. public class One {
  6. /**
  7. *查看某个文件夹下所有的文件
  8. * @param args
  9. */
  10. public static void main(String[] args) {
  11. /*File file1 = new File("D:\\e\\test.txt");
  12. delete(file1);
  13. File file = new File("D:" + File.separator);
  14. print(file);*/
  15. //"D:\\门头沟国高新2018年度(管委).xlsx"
  16. try {
  17. download("门头沟国高新2018年度(管委).xlsx");
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. /* String s = "D:/e/D:/门头沟国高新2018年度(管委).xlsx (文件名、目录名或卷标语法不正确。)";
  22. String substring = s.substring(8, s.length());
  23. System.out.println(substring);*/
  24. }
  25. /**
  26. * 打印某个文件夹下的所有文件名字
  27. * @param file
  28. */
  29. public static void print(File file){
  30. if (file != null){
  31. if (file.isDirectory()){
  32. File[] files = file.listFiles();
  33. if (files != null){
  34. for (int i = 0; i < files.length; i++){
  35. System.out.println(files[i]);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. /**
  42. * 删除某个文件
  43. * @param file
  44. */
  45. public static void delete(File file){
  46. if (file != null && file.exists()){
  47. boolean delete = file.delete();
  48. System.out.println(delete);
  49. }
  50. }
  51. /**
  52. * 下载某个文件
  53. * @param fileName
  54. * @throws IOException
  55. */
  56. public static void download(String fileName) throws IOException {
  57. FileOutputStream fileOutputStream = null;
  58. FileInputStream fileInputStream = null;
  59. try {
  60. fileInputStream = new FileInputStream("D:\\" + fileName);
  61. fileOutputStream = new FileOutputStream("D:\\e\\" + fileName);
  62. } catch (FileNotFoundException e) {
  63. e.printStackTrace();
  64. }
  65. BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
  66. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
  67. byte[] bytes = new byte[1024];
  68. while (bufferedInputStream.read(bytes) != -1){
  69. bufferedOutputStream.write(bytes);
  70. }
  71. bufferedOutputStream.flush();
  72. bufferedOutputStream.close();
  73. bufferedInputStream.close();
  74. }
  75. }

 

版权声明:本文为prader6原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/prader6/p/11363577.html