我正在尝试了解 Spring 的 I/O 基础知识。
我正在研究文件上传示例。我想将文件存储在项目中的临时文件夹中以进行测试。
我有这个代码:
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
//get directory
stream.write(bytes);
stream.close();
我在项目文件中创建了文件夹“files”
。我想知道如何定位本地文件夹。
我尝试使用类File
的备用构造函数。
最佳答案
在外部(来自 Web 容器)目录中写入文件的唯一方法是使用文件的完整直接路径。只需在以下 File 构造函数中使用完整的文件路径即可。
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
关于java - 将文件上传到本地 Eclipse 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28754742/