宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動態(tài)
精選推薦

Spring Boot 2.x基礎(chǔ)教程:實現(xiàn)文件上傳

管理 管理 編輯 刪除

第一步:創(chuàng)建一個基礎(chǔ)的Spring Boot項目,如果還不會的話就先看看這篇《快速入門》open in new window

第二步:在pom.xml中引入模版引擎依賴:


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

你也可以選擇其他你熟悉的模版引擎,比如:Freemarker。

第三步:在resources目錄下,創(chuàng)建新目錄templates;在templates目錄下再創(chuàng)建一個文件上傳的頁面upload.html,內(nèi)容如下:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>文件上傳頁面</title>
</head>
<body>
<h1>文件上傳頁面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
    選擇要上傳的文件:<input type="file" name="file"><br>
    <hr>
    <input type="submit" value="提交">
</form>
</body>
</html>

第四步:創(chuàng)建文件上傳的處理控制器,命名為UploadController


@Controller
@Slf4j
public class UploadController {

    @Value("${file.upload.path}")
    private String path;

    @GetMapping("/")
    public String uploadPage() {
        return "upload";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String create(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String filePath = path + fileName;

        File dest = new File(filePath);
        Files.copy(file.getInputStream(), dest.toPath());
        return "Upload file success : " + dest.getAbsolutePath();
    }

}

其中包含這幾個重要元素:

  1. 成員變量path,通過@Value注入配置文件中的file.upload.path屬性。這個配置用來定義文件上傳后要保存的目錄位置。
  2. GET請求,路徑/,用于顯示upload.html這個文件上傳頁面。
  3. POST請求。路徑/upload,用于處理上傳的文件,即:保存到file.upload.path配置的路徑下面。
注意:這里主要演示文件上傳的主要流程,真實應(yīng)用還有更多內(nèi)容要考慮,比如:文件上傳后的文件名處理(防止重名)、分布式情況下文件上傳后如何共享訪問等。更高級的最后,我們后續(xù)文章繼續(xù)講。

第五步:編輯application.properties配置文件


spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB

file.upload.path=/Users/didi/

前兩個參數(shù)用于限制了上傳請求和上傳文件的大小,而file.upload.path是上面我們自己定義的用來保存上傳文件的路徑。

更多本系列免費教程連載「點擊進入?yún)R總目錄」

#測試驗證

第一步:啟動Spring Boot應(yīng)用,訪問http://localhost:8080,可以看到如下的文件上傳頁面。

第二步:選擇一個不大于2MB的文件,點擊“提交”按鈕,完成上傳。

如果上傳成功,將顯示類似下面的頁面:

你可以根據(jù)打印的文件路徑去查看文件是否真的上傳了。

#代碼示例

本文的相關(guān)例子可以查看下面?zhèn)}庫中的chapter4-3目錄:

請登錄后查看

CRMEB 最后編輯于2025-02-27 14:51:31

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認正序 回復(fù)倒序 點贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
750
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產(chǎn)品動態(tài) 精選推薦 首頁頭條 首頁動態(tài) 首頁推薦
取 消 確 定
回復(fù)
回復(fù)
問題:
問題自動獲取的帖子內(nèi)容,不準確時需要手動修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認打賞

微信登錄/注冊

切換手機號登錄

{{ bind_phone ? '綁定手機' : '手機登錄'}}

{{codeText}}
切換微信登錄/注冊
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服