上傳代碼
public ResultInfo<?> uploadFile(@RequestParam MultipartFile file, @RequestParam String id) throws BusinessException{
try {
if (file.isEmpty()) {
return JsonResult.error(StatusCode.ERROR_ADD);
}
// 獲取文件名
String fileName = file.getOriginalFilename();
System.out.println("上傳的文件名為:" + fileName);
String preName = fileName.substring(0,fileName.lastIndexOf("."));
// 獲取文件的后綴名
String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println("文件的后綴名為:" + suffixName);
// 設(shè)置文件存儲(chǔ)路徑 *************************************************
String filePath = "自己的文件路徑/";
String path = filePath + fileName;
File dest = new File(new File(path).getAbsolutePath());// dist為文件,有多級(jí)目錄的文件
// 檢測(cè)是否存在目錄
if (dest.getParentFile().exists()) {//因此這里使用.getParentFile(),目的就是取文件前面目錄的路徑
//如果存在文件夾
FileSystemUtils.deleteRecursively(dest.getParentFile());// 刪除文件夾
}
dest.getParentFile().mkdirs();// 新建文件夾
file.transferTo(dest);// 文件寫入
return this.update(actionData);
} catch (IllegalStateException e) {
e.printStackTrace();
throw new BusinessException("999999", "業(yè)務(wù)數(shù)據(jù)操作異常", e);
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException("999999", "業(yè)務(wù)數(shù)據(jù)操作異常", e);
}
}
文件下載代碼文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-534426.html
public ResultInfo<?> downloadFile(String fileName , HttpServletResponse response) throws BusinessException{
if (fileName != null) {
//設(shè)置文件路徑
String filePath = "自己的文件路徑"+"/"+fileName;
File file = new File(filePath);
if (file.exists()) {
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 設(shè)置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
return JsonResult.success(bis);
} catch (Exception e) {
e.printStackTrace();
} finally { // 做關(guān)閉操作
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return JsonResult.error(StatusCode.ERROR_ADD);
}
后端通過(guò)json拿到文件流的返回,我采用將文件流轉(zhuǎn)成base64返回給前臺(tái),實(shí)現(xiàn)代碼如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-534426.html
public ResultInfo<?> downloadFile(String fileName ) throws BusinessException{
if (fileName != null) {
//設(shè)置文件路徑
String filePath = "自己的文件路徑/"+fileName;
String base64 = null;
InputStream in = null;
try {
File file = new File(filePath);
in = new FileInputStream(file);
byte[] bytes=new byte[(int)file.length()];
in.read(bytes);
base64 = Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException("999999", "業(yè)務(wù)數(shù)據(jù)操作異常", e);
}finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException("999999", "業(yè)務(wù)數(shù)據(jù)操作異常", e);
}
}
}
return JsonResult.success(base64);
}
return JsonResult.error(StatusCode.ERROR_ADD);
}
到了這里,關(guān)于java實(shí)現(xiàn)文件的上傳和下載,將文件流轉(zhuǎn)base64返回給前端的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!