Get file path using ClassPathResource

This commit is contained in:
TobeHyo
2021-09-13 14:48:54 +09:00
parent bf9e104319
commit e0f580c9bb
4 changed files with 35 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
package com.tobesoft.overseas.nexacrodemo.config;
import java.io.File;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
@@ -12,6 +13,7 @@ import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
public class NexacroXeniConfiguration {
@@ -23,12 +25,20 @@ public class NexacroXeniConfiguration {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// set nexacro-xeni context params
String realPath = new File("src/main/resources/static").getAbsolutePath();
String exportPath = "file://" + realPath + "\\export";
String importPath = "file://" + realPath + "\\import";
String realPath = "";
try {
realPath = new ClassPathResource("static").getFile().getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
String exportPath = "file://" + realPath + File.separator + "export";
String importPath = "file://" + realPath + File.separator + "import";
// String exporURL = "http://localhost:8080/nexacrodemo/XExportImport";
servletContext.setInitParameter("export-path", exportPath);
servletContext.setInitParameter("import-path", importPath);
// servletContext.setInitParameter("export-url", exporURL);
servletContext.setInitParameter("import-path", importPath);
servletContext.setInitParameter("monitor-enabled", "true");
servletContext.setInitParameter("monitor-cycle-time", "5");
servletContext.setInitParameter("file-storage-time", "3");

View File

@@ -29,6 +29,7 @@ import com.nexacro.java.xapi.tx.PlatformType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -45,8 +46,8 @@ public class NexacroFileUploadDownloadControl {
private static final String CHAR_SET = "UTF-8";
private String getFilePath(String userFolder) {
String realPath = new File("src/main/resources/static").getAbsolutePath();
private String getFilePath(String userFolder) throws IOException {
String realPath = new ClassPathResource("static").getFile().getAbsolutePath();
String uploadPath = realPath + SP + PATH + SP + userFolder;
log.debug(uploadPath);
File extFolder = new File(uploadPath);
@@ -93,7 +94,12 @@ public class NexacroFileUploadDownloadControl {
String userFileFolder = inVariableList.getString("filefolder");
String uploadPath = getFilePath(userFileFolder);
String http = request.getHeader("X-Forwarded-Proto");
String host = request.getHeader("Host");
String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/services")) + "/" + PATH + "/" +userFileFolder + "/";
if(http != null && host != null) {
url = http + "://" + host + request.getRequestURL().substring(request.getRequestURL().lastIndexOf("/services"), request.getRequestURL().lastIndexOf("/services")) + "/" + PATH + "/" +userFileFolder + "/";
}
List<File> fileList = new ArrayList<>();
File directory = new File(uploadPath);
@@ -165,7 +171,12 @@ public class NexacroFileUploadDownloadControl {
}
File destination = new File(filePath + SP + fileName);
multipartFile.transferTo(destination);
String http = request.getHeader("X-Forwarded-Proto");
String host = request.getHeader("Host");
String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/services")) + "/" + PATH + "/" +userFileFolder + "/";
if(http != null && host != null) {
url = http + "://" + host + request.getRequestURL().substring(request.getRequestURL().lastIndexOf("/services"), request.getRequestURL().lastIndexOf("/services")) + "/" + PATH + "/" +userFileFolder + "/";
}
row = ds.newRow(0);
ds.set(row, "fileName", fileName);
ds.set(row, "fileSize", destination.length());

View File

@@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -18,6 +19,7 @@ import com.nexacro.java.xapi.tx.PlatformRequest;
import com.nexacro.java.xapi.tx.PlatformResponse;
import com.nexacro.java.xapi.tx.PlatformType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -65,8 +67,8 @@ public class NexacroLargeDataController {
}
int nRow;
String path = new File("src/main/resources/dat/ssv_" + rowCount + ".dat").getAbsolutePath();
BufferedReader br = new BufferedReader(new FileReader(path));
File file = new ClassPathResource("dat/ssv_" + rowCount + ".dat").getFile();
BufferedReader br = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
String line;
char a = (char) 0x1e;

View File

@@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -18,6 +19,7 @@ import com.nexacro.java.xapi.tx.PlatformRequest;
import com.nexacro.java.xapi.tx.PlatformResponse;
import com.nexacro.java.xapi.tx.PlatformType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -68,8 +70,8 @@ public class NexacroPivotDataController {
}
int nRow;
String path = new File("src/main/resources/dat/test" + rowCount + ".dat").getAbsolutePath();
BufferedReader br = new BufferedReader(new FileReader(path));
File file = new ClassPathResource("dat/test" + rowCount + ".dat").getFile();
BufferedReader br = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
String line;
char a = (char) 0x1e;