Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

添加根据代码运行的操作系统自动创建文件上传根目录功能 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ramostear merged 1 commit into ramostear:master from gaohanghang:master
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ramostear.application.controller;

import com.ramostear.application.model.FileInfo;
import com.ramostear.application.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
Expand All @@ -25,9 +26,16 @@
@Controller
public class FileController {

private static String fileUploadRootDir = null;

@Value ( "${file.upload.root.dir}" )
String fileUploadRootDir;
@Value ( "${file.upload.root.dir.windows}" )
String fileUploadRootDirWindows;

@Value ( "${file.upload.root.dir.mac}" )
String fileUploadRootDirMac;

@Value ( "${file.upload.root.dir.linux}" )
String fileUploadRootDirLinux;

private static Map<String,FileInfo> fileRepository = new HashMap<>();

Expand All @@ -39,6 +47,20 @@ public void initFileRepository(){
fileRepository.put ( file1.getName (),file1 );
fileRepository.put ( file2.getName (),file2 );
fileRepository.put ( file3.getName (),file3 );

// 判断文件夹是否存在,不存在就创建
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS")) {
// 苹果
fileUploadRootDir = fileUploadRootDirMac;
} else if (osName.startsWith("Windows")) {
// windows
fileUploadRootDir = fileUploadRootDirWindows;
} else {
// unix or linux
fileUploadRootDir = fileUploadRootDirLinux;
}
FileUtil.createDirectories(fileUploadRootDir);
}

@GetMapping("/files")
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/ramostear/application/util/FileUtil.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ramostear.application.util;

import java.io.File;

public class FileUtil {
public static void createDirectories(String pathname) {
File directories = new File(pathname);
if (directories.exists()) {
System.out.println("文件上传根目录已存在");
} else { // 如果目录不存在就创建目录
if (directories.mkdirs()) {
System.out.println("创建多级目录成功");
} else {
System.out.println("创建多级目录失败");
}
}
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ spring.freemarker.suffix=.html
spring.freemarker.enabled=true
spring.freemarker.charset=UTF-8
spring.freemarker.template-loader-path=classpath:/templates/
file.upload.root.dir = C:/work/upload/
file.upload.root.dir.windows = C:/work/upload/
file.upload.root.dir.mac = ~/upload/
file.upload.root.dir.linux = ~/upload/

AltStyle によって変換されたページ (->オリジナル) /