我正在向用户显示特定文件系统目录中的文件表。 我希望用户能够单击这些外部html文件并进行查看。现在,我知道在Spring中,我必须提供另一个GetMapping或此RequestMapping才能获取文件的filePath,然后传递文件,但是我得到了
java.lang.IllegalArgumentException:在请求目标中找到无效字符。有效字符在RFC 7230和RFC 3986中定义。
我尝试使用: 在我的directory.html中 但是当我单击链接时(即使URL正确),也没有任何反应。
此活动有任何提示吗?
public class DirectoryFile /*implements Serializable */{
public static final long serialVersionUID = 1L;
private String fileName;
private String filePath;
public DirectoryFile(String fileName, String filePath) {
this.fileName = fileName;
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
我的Thymeleaf directory.html的一部分:
<table>
<tr><th>File Name</th>
</tr>
<tr th:each="file : ${filedata}">
<td><a th:href="@{/ebay/viewfile?filename=${file.filePath}}"><span th:text="${file.fileName}"/></a></td>
控制器:
@Controller
@RequestMapping("ebay") // Path to controller
public class FileController {
@Autowired
FileService fileService;
@GetMapping(Mappings.FILES)
public String getDirectory(Model model){
model.addAttribute(AttributeNames.FILE_DATA, fileService.getDirectory());
return ViewNames.DIRECTORY;
}
@RequestMapping(value="/ebay/viewfile", method=RequestMethod.GET)
@ResponseBody
public String getFile(@RequestParam(value="filename") String filename){
return filename;
}
}
FileSystemProperties:
@Component
public class FileSystemProperties {
private File fileDirectory = new File("M:\\server\\spring\\ebayfiles\\"); // Todo This needs to be customizable ->
public File getFileDirectory() {
return fileDirectory;
}
public void setFileDirectory(File fileDirectory) {
this.fileDirectory = fileDirectory;
}
}
FileServiceImpl:
@Override
public List<DirectoryFile> getDirectory() {
List<DirectoryFile> directoryFileList = new ArrayList<>();
for(final File fileEntry: fsProperties.getFileDirectory().listFiles()){
if(!fileEntry.isDirectory()){
DirectoryFile directoryFile = new DirectoryFile(
fileEntry.getName(), fileEntry.getPath()
);
directoryFileList.add(directoryFile);
}
}
return directoryFileList;
}