顯示具有 SpringBoot 標籤的文章。 顯示所有文章
顯示具有 SpringBoot 標籤的文章。 顯示所有文章

SpringBoot 檔案上傳與下載

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件上传和下载</title>
</head>
<body>
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
<p>
<label for="file1" >请选择要上传的文件</label>
<input type="file" id="file1" name="file" multiple/>
</p>
<p>
<input type="submit" value="上传">
</p>
</form>
<ul>
<li th:each="file : ${files}">
<a th:href="@{/download(fileName=${file})}" th:text="${file}"></a>
</li>
</ul>
</body>
</html>
 package com.sun.ch04.controller;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.FileUtils;

@Controller
public class FileController {
@GetMapping("/upload")
public String doUpload(Model model) {
// 将上传文件目录下的所有文件名列出来,保存到模型对象中
String uploadPath = "C:" + File.separator + "SpringBootUpload";
File dir = new File(uploadPath);
model.addAttribute("files", dir.list());
return "upload";
}

@PostMapping("/upload")
@ResponseBody
public String upload(HttpServletRequest request) {
// 得到所有文件的列表
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
String uploadPath = "C:" + File.separator + "SpringBootUpload";
File dir = new File(uploadPath);

// 如果保存上传文件的目录不存在,则创建它
if (!dir.exists()) {
dir.mkdirs();
}
for (MultipartFile f : files) {
if (f.isEmpty()) {
continue;
}
File target = new File(uploadPath + File.separator + f.getOriginalFilename());
try {
f.transferTo(target);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}
return "文件上传成功!";
}

@GetMapping("/download")
public ResponseEntity<byte[]> download(@RequestParam String fileName) throws IOException {
String dir = "C:" + File.separator + "SpringBootUpload";
String fileFullPath = dir + File.separator + fileName;
File file = new File(fileFullPath);

ResponseEntity.BodyBuilder builder = ResponseEntity.ok();
builder.contentLength(file.length());
builder.contentType(MediaType.APPLICATION_OCTET_STREAM);
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
builder.header("Content-Disposition", "attachment; filename=" + fileName);
return builder.body(FileUtils.readFileToByteArray(file));
}

}

SpringBoot Maven pom基本介紹2


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--專案識別-->
<!--版本-->
<modelVersion>4.0.0</modelVersion>
<!--.公司.組織-->
<groupId>org.imi</groupId>
<!--專案名-->
<artifactId>myDemo</artifactId>
<!--版本 SNAPSHOT為不穩定版本-->
<version>1.0.0</version>
<packaging>war</packaging>
<name>myDemo</name>
<description>這是一個demo用的專案</description>

<!--變數設置-->
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!--繼承springframework-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<!--多環境建置-->
<profiles>
<!--開發環境-->
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<!--默認環境-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--正式環境-->
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>

<!--設定引用函式庫-->
<dependencies>
<!--spring-boot Web服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--spring-boot thymeleaf模版引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!--spring-boot security安全守護-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- &lt;!&ndash;spring-boot jpa 資料庫&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->

<!-- &lt;!&ndash;spring-boot test單元測試&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->


<!-- &lt;!&ndash;logback&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>ch.qos.logback</groupId>-->
<!-- <artifactId>logback-classic</artifactId>-->
<!-- <version>1.2.3</version>-->
<!-- </dependency>-->
</dependencies>

<!--建置專案所需要資訊-->
<build>
<finalName>${project.artifactId}</finalName>
<!--專案使用的程式列表-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.7</version>
</plugin>
</plugins>

<resources>
<!-- <resource>-->
<!-- <directory>src/main/java</directory>-->
<!-- <includes>-->
<!-- <include>**/*.xml</include>-->
<!-- </includes>-->
<!-- <filtering>false</filtering>-->
<!-- </resource>-->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>ajax/</include>
<include>static/</include>
<!-- <include>**/*.xml</include>-->
</includes>
</resource>
<!--多環境配置-->
<resource>
<directory>src/main/resources</directory>
<!-- < filtering >true</ filtering >其含义是扫描src/main/resources/下的所有propertiesxml文件将其中的${}引用在打包时换成直接引用。-->
<filtering>true</filtering>
<includes>
<!-- 项目打包完成的包中只包含当前环境文件 -->
<include>application.properties</include>
<include>application-${activatedProperties}.properties</include>
</includes>
<!-- <excludes>-->
<!-- <exclude>static/font/**</exclude>-->
<!-- </excludes>-->
</resource>
</resources>
</build>
</project>

SpringBoot Maven pom基本介紹

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--專案識別-->
<!--版本-->
<modelVersion>4.0.0</modelVersion>
<!--.公司.組織-->
<groupId>org.imi</groupId>
<!--專案名-->
<artifactId>myDemo</artifactId>
<!--版本 SNAPSHOT為不穩定版本-->
<version>1.0.0</version>
<packaging>war</packaging>
<name>myDemo</name>
<description>這是一個demo用的專案</description>


<!--變數設置-->
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!--繼承springframework-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<!--設定引用函式庫-->
<dependencies>
<!--spring-boot Web服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<!--建置專案所需要資訊-->
<build>
<!--專案使用的程式列表-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>