自己再做文件解壓縮相關的功能時 接受文件流後 將文件通過相對路徑直接生成,但是找遍了項目都沒有:
@ResponseBody
@RequestMapping(value = "/zipMutilfile", method = {RequestMethod.GET, RequestMethod.POST })
public void zipMutilfile(HttpServletResponse response, @RequestParam("file") MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
System.out.println(originalFilename);
PrintWriter writer = response.getWriter();
File f = null;
if(file.equals("")||file.getSize()<=0){
file = null;
}else{
InputStream ins = file.getInputStream();
f=new File(originalFilename);
System.out.println("文件接收開始。。");
ZipUtil.inputStreamToFile(ins, f);//生成對應文件
System.out.println("文件接收結束!");
}
}
//輸入流轉文件
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024*10];
while ((bytesRead = ins.read(buffer, 0, 10240)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
後來發現文件到了,卻找不到文件在哪
直接去項目文件中去找,看看是不是沒有刷新過來
但是還是沒有,
才發現 原來ide並沒有將項目佈置到tomcat中
而是通過配置將路徑給了tomcat 才啓動的項目
靈光一閃,啓動時在bin中啓動的
找到你了
小夥子藏的挺開心的。。
同時測試了下:
File f2 = new File(".");
String absolutePath = f2.getAbsolutePath();
System.out.println("absolutePath:"+absolutePath);//D:\..\bin\.
可以看出 的確是在bin中爲起始位置
於是老方法:改成獲取項目根路徑
String s = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
String path = s.split("WEB-INF/classes/")[0];
文件成功轉移到項目上去了
這裏如果想放入out中的文件夾,可能有同學會發現,自己新建的文件夾沒有生成到out目錄,導致出現沒有找到路徑錯誤
不用慌,空文件夾不會生成到out目錄中的,可以填個說明文檔,或者直接向我這樣放在根目錄底下(我後期解壓後還會刪除這個文件,對位置無所謂的)