問題
- spring-boot的項目,雖然它自己定義了一個靜態(tài)文件的存儲目錄,但是這個目錄一般是作為前端靜態(tài)文件的目錄來作為使用的。如果使用這個靜態(tài)目錄來作為我們上傳文件的目錄會有一個比較尷尬的地方:將spring-boot打包成為jar包后,隨著上傳圖片的增多,這個jar包也會跟著變大,而且,在jar包中的圖片要單獨取出來查看或者編輯都極不方便。
- 因此我們需要另外定義一個文件目錄來單獨存儲我們上傳文件的目錄。
- 這里是基礎教學,大神請繞路,如果看到后有什么比較好的意見也歡迎評論區(qū)指出。以下以自己的一個個人項目來作為演示,有不清楚或有什么問題的地方,也請評論區(qū)指出。
上傳
此處的范圍類型 AjaxResult 是一個繼承 HashMap 類,從若依框架上拿過來的。至于包名,我也沒改,自己改一下吧
HttpStatus 類
package com.example.xixieguan.utils;
/**
* 返回狀態(tài)碼
*
* @author ruoyi
*/
public class HttpStatus
{
/**
* 操作成功
*/
public static final int SUCCESS = 200;
/**
* 對象創(chuàng)建成功
*/
public static final int CREATED = 201;
/**
* 請求已經(jīng)被接受
*/
public static final int ACCEPTED = 202;
/**
* 操作已經(jīng)執(zhí)行成功,但是沒有返回數(shù)據(jù)
*/
public static final int NO_CONTENT = 204;
/**
* 資源已被移除
*/
public static final int MOVED_PERM = 301;
/**
* 重定向
*/
public static final int SEE_OTHER = 303;
/**
* 資源沒有被修改
*/
public static final int NOT_MODIFIED = 304;
/**
* 參數(shù)列表錯誤(缺少,格式不匹配)
*/
public static final int BAD_REQUEST = 400;
/**
* 未授權
*/
public static final int UNAUTHORIZED = 401;
/**
* 訪問受限,授權過期
*/
public static final int FORBIDDEN = 403;
/**
* 資源,服務未找到
*/
public static final int NOT_FOUND = 404;
/**
* 不允許的http方法
*/
public static final int BAD_METHOD = 405;
/**
* 資源沖突,或者資源被鎖
*/
public static final int CONFLICT = 409;
/**
* 不支持的數(shù)據(jù),媒體類型
*/
public static final int UNSUPPORTED_TYPE = 415;
/**
* 系統(tǒng)內(nèi)部錯誤
*/
public static final int ERROR = 500;
/**
* 接口未實現(xiàn)
*/
public static final int NOT_IMPLEMENTED = 501;
/**
* 系統(tǒng)警告消息
*/
public static final int WARN = 601;
}
AjaxResult類
package com.example.xixieguan.utils;
import java.util.HashMap;
public class AjaxResult extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/** 狀態(tài)碼 */
public static final String CODE_TAG = "code";
/** 返回內(nèi)容 */
public static final String MSG_TAG = "msg";
/** 數(shù)據(jù)對象 */
public static final String DATA_TAG = "data";
/**
* 初始化一個新創(chuàng)建的 AjaxResult 對象,使其表示一個空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一個新創(chuàng)建的 AjaxResult 對象
*
* @param code 狀態(tài)碼
* @param msg 返回內(nèi)容
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一個新創(chuàng)建的 AjaxResult 對象
*
* @param code 狀態(tài)碼
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
*/
public AjaxResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
// super.put(DATA_TAG, data);
if (data!=null)
{
super.put(DATA_TAG, data);
}
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success()
{
return AjaxResult.success("操作成功");
}
/**
* 返回成功數(shù)據(jù)
*
* @return 成功消息
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回內(nèi)容
* @return 成功消息
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回警告消息
*
* @param msg 返回內(nèi)容
* @return 警告消息
*/
public static AjaxResult warn(String msg)
{
return AjaxResult.warn(msg, null);
}
/**
* 返回警告消息
*
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 警告消息
*/
public static AjaxResult warn(String msg, Object data)
{
return new AjaxResult(HttpStatus.WARN, msg, data);
}
/**
* 返回錯誤消息
*
* @return 錯誤消息
*/
public static AjaxResult error()
{
return AjaxResult.error("操作失敗");
}
/**
* 返回錯誤消息
*
* @param msg 返回內(nèi)容
* @return 錯誤消息
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
* 返回錯誤消息
*
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 錯誤消息
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回錯誤消息
*
* @param code 狀態(tài)碼
* @param msg 返回內(nèi)容
* @return 錯誤消息
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
/**
* 方便鏈式調(diào)用
*
* @param key 鍵
* @param value 值
* @return 數(shù)據(jù)對象
*/
@Override
public AjaxResult put(String key, Object value)
{
super.put(key, value);
return this;
}
}
controller類
package com.example.xixieguan.controller;
import com.example.xixieguan.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
@RestController
@RequestMapping("/api")
public class FileUploadController {
private String separator = File.separator;//獲取操作系統(tǒng)的文件分隔符
@Autowired
private ResourceLoader resourceLoader;
@PostMapping("/upload")
public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException {
// 判斷是否為空文件
AjaxResult ajaxResult = AjaxResult.error();
ajaxResult.put("msg","上傳失敗");
if (file.isEmpty()) {
return ajaxResult;
}
// 獲取文件名
String fileName = file.getOriginalFilename();
// 獲取文件后綴
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 重新生成文件名
fileName = UUID.randomUUID() + suffixName;
// 設置文件存儲路徑
Path currentDir = Paths.get("images");
Path filePath = currentDir.toAbsolutePath();
if (!Files.exists(filePath)) {
Files.createDirectory(filePath);
}
File dest = new File(filePath + separator + fileName);
try {
// 保存文件
file.transferTo(dest);
ajaxResult = AjaxResult.success();
ajaxResult.put("msg","上傳成功");
return ajaxResult;
} catch (IOException e) {
e.printStackTrace();
}
return ajaxResult;
}
}
添加web配置類
- 建議添加在config包下
- images是我自己定義用來存儲上傳文件的目錄名,你可以根據(jù)自己的需求來定制。但是需要注意的是在上傳的時候也需要是同樣的目錄名,不然會出現(xiàn)找不到目錄的尷尬場面。
package com.example.xixieguan.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class StaticResourceConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**")
.addResourceLocations("file:./images/");
}
}
完成以上兩步,已經(jīng)可以將圖片上傳到自定義的目錄位置了,如果不行的話,在application.yml中再添加一項配置文章來源:http://www.zghlxwxcb.cn/news/detail-587658.html
application.yml
spring:
web:
resources:
static-locations: file:./image/
通過上述方式,已經(jīng)基本可以完成 Java spring-boot項目上傳下載文件或圖片的功能需求,關鍵在于不用將文件放置到spring-boot的靜態(tài)目錄下了。減少了維護的麻煩事兒。文章來源地址http://www.zghlxwxcb.cn/news/detail-587658.html
到了這里,關于Java spring-boot項目中如何上傳下載文件或圖片到spring-boot規(guī)定的非靜態(tài)目錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!