中学网站asp模板建网站源码建站详解
news/
2025/9/23 22:40:14/
文章来源:
中学网站asp模板,建网站源码建站详解,怎么搭建源码网站,公司flash网站模板文章目录 一、原理分析1.1 请求类型1.2 服务器解析 二、功能实现2.1 创建项目并导入依赖2.2 文件上传功能实现2.2.1 文件上传 Service2.2.2 文件上传 Controller 2.3 文件下载功能实现2.3.1 文件下载 Service2.3.2 文件下载 Controller 2.4 文件上传前端代码(可选)2.4.1 上传文… 文章目录 一、原理分析1.1 请求类型1.2 服务器解析 二、功能实现2.1 创建项目并导入依赖2.2 文件上传功能实现2.2.1 文件上传 Service2.2.2 文件上传 Controller 2.3 文件下载功能实现2.3.1 文件下载 Service2.3.2 文件下载 Controller 2.4 文件上传前端代码(可选)2.4.1 上传文件的 HTML 表单2.4.2 访问前端页面 三、功能测试3.1 测试文件上传3.2 测试文件下载 参考资料 完整案例代码java-demos/spring-boot-demos/spring-boot-file at main · idealzouhu/java-demos (github.com) 一、原理分析
1.1 请求类型
文件上传通常使用 multipart/form-data 类型的 POST 请求。
multipart/form-data用于在表单提交时上传文件的 MIME 类型。它允许将文件和其他表单字段组合在一起发送服务器能识别出每个部分并提取出文件。
文件下载则只是简单的 GET 请求。在 Spring MVC 中返回对象通常是 ResponseEntity 或者 HttpServletResponse 对象。
1.2 服务器解析
现有的 Web 框架都内置了处理文件上传的功能。在 Spring MVC 中 MultipartFile 是用来表示上传的文件服务器会自动解析该文件并进行处理。
MultipartFile 的常用方法有
getName()获取文件在服务器上的文件名可能已经被服务器修改。getOriginalFilename()获取文件在客户端上的原始文件名没有被服务器修改。getContentType()获取文件的内容类型。isEmpty()判断文件是否为空。getSize()获取文件大小字节。getBytes()读取文件内容为字节数组。getInputStream()获取文件内容的输入流。getResource()将文件转换为资源对象。transferTo(File dest)将文件内容传输到指定的目标文件。transferTo(Path dest)将文件内容传输到指定的目标路径
二、功能实现
2.1 创建项目并导入依赖
在 start.springboot.io 创建项目导入以下依赖。
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency2.2 文件上传功能实现
将文件上传的具体逻辑抽象到一个 FileService 中让 Controller 调用该服务处理上传请求。
2.2.1 文件上传 Service
Service
public class FileService {/*** 上传文件** param file* return*/public String uploadFile(MultipartFile file) {// 检查文件是否为空if (file.isEmpty()) {return 上传失败因为文件是空的。;}// 获取文件名和保存路径String fileName file.getOriginalFilename();String filePath C:/uploads/; // 自定义文件保存路径File dest new File(filePath fileName);// 确保目录存在if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}// 保存文件try {file.transferTo(dest);return 文件上传成功 fileName;} catch (IOException e) {e.printStackTrace();return 文件上传失败。;}}
}2.2.2 文件上传 Controller
RequiredArgsConstructor
RestController
public class FileUploadController {private final FileService fileService;/*** 上传文件自动绑定到 MultipartFile 对象中** param file 上传文件* return*/PostMapping(/upload)public String upload(RequestParam(file) MultipartFile file) {return fileService.uploadFile(file);}
}2.3 文件下载功能实现
将文件上传的具体逻辑抽象到一个 FileService 中让 Controller 调用该服务处理上传请求。
2.3.1 文件下载 Service
Service
public class FileService {/*** 下载文件** 根据文件名构建文件路径并检查文件是否存在如果文件不存在则返回404未找到的响应* 如果文件存在将文件作为资源包装并设置HTTP响应头以提示浏览器下载文件** param fileName 要下载的文件名* return 包含文件资源的响应实体如果文件不存在则为404响应*/public ResponseEntityResource downloadFile(String fileName) {// 构建文件路径String filePath C:/uploads/ fileName;File file new File(filePath);// 检查文件是否存在if (!file.exists()) {// 文件不存在返回404未找到return ResponseEntity.notFound().build();}// 将文件包装为资源Resource resource new FileSystemResource(file);// 创建HTTP响应头用于指定文件下载HttpHeaders headers new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, attachment; filename\ fileName \);// 返回包含文件资源和响应头的响应实体return ResponseEntity.ok().headers(headers).body(resource);}
}2.3.2 文件下载 Controller
RequiredArgsConstructor
RestController
public class FileDownloadController {private final FileService fileService;GetMapping(/download)public ResponseEntityResource downloadFile(RequestParam String fileName) {return fileService.downloadFile(fileName);}
}2.4 文件上传前端代码(可选)
该部分代码为 [2.2 文件上传功能实现](###2.2 文件上传功能实现) 的前端实现。
2.4.1 上传文件的 HTML 表单
创建 upload.html 文件放在 src/main/resources/static 目录下。
!DOCTYPE html
html
headtitleFile Upload/title
/head
bodyh2Upload a file/h2form iduploadFormlabel forfileChoose file to upload:/labelinput typefile idfile namefile required /brbrbutton typesubmitUpload/button
/formscriptdocument.getElementById(uploadForm).addEventListener(submit, function(event) {event.preventDefault(); // 防止表单的默认提交行为const fileInput document.getElementById(file);const formData new FormData();formData.append(file, fileInput.files[0]);// 使用 Fetch API 发送文件到后端fetch(/upload, {method: POST,body: formData}).then(response response.text()).then(data alert(File uploaded successfully: data)).catch(error console.error(Error uploading file:, error));});
/script/body
/html其中表单中 namefile 指定了文件在请求体中的键后端服务器将通过这个名称来获取文件数据。
2.4.2 访问前端页面
进入前端页面的 URL 为 http://localhost:8080/upload.html 三、功能测试
3.1 测试文件上传
使用 Postman 发送 POST 请求到 /upload并选择一个文件进行上传。示例 URL 为
http://localhost:8080/upload 3.2 测试文件下载
使用 Postman 或 HTML 表单发送 POST 请求下载指定的文件。
示例 URL 为 http://localhost:8080/download?fileNamedemo.txt 参考资料
Java实战Spring Boot 实现文件上传下载功能_springboot 文件下载-CSDN博客
Spring Boot文件上传与下载讲解与实战超详细 附源码-阿里云开发者社区 (aliyun.com)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/914120.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!