springboot統合ウェブ(B)-----------ファイルのアップロード

新springbootプロジェクト

https://blog.csdn.net/qq_43560721/article/details/104653470

プロジェクト構造

コントローラの作成

package cn.xxs.springbootfileupload.controller;
import java.io.File;
import java.io.IOException;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController//当前类下的方法返回值自动做json格式的转换
public class FileUploadController {
    /**
     * 文件上传
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file) throws IllegalStateException, IOException{
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("E:/"+file.getOriginalFilename()));
        return "success";
    }
}

スタートライティングクラス

package cn.xxs.springbootfileupload;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootfileuploadApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootfileuploadApplication.class, args);
    }

}

application.propertiesファイルのパラメータと権限アクセス・パスにファイルをアップロード


spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static

2以下に提供することはできませんが、いくつかのバージョンは、次の404を設定することはできません、私はこのようなものでした

 

セットアップロードページ

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
	<input type="file" name="file"/>
	<br>
	<input type="submit" value="上传">
</form>
</body>
</html>

Accessプロジェクト

ブラウザと入力します。http:// localhostを:8080 /静的 / upload.html

 

ここで私はミスを犯しました

図怠惰直接アクセス

ブラウザに飛び込んだ、あなたがダウンして行く見に注意を払っています(慎重に使用すると、ポート番号が8080ではないでしょう見ます)

つながります

 

 

公開された141元の記事 ウォン称賛33 ビュー50000 +

おすすめ

転載: blog.csdn.net/qq_43560721/article/details/104949909