欢迎光临散文网 会员登陆 & 注册

weeq

2023-07-03 00:22 作者:shangxan  | 我要投稿

以下是一个使用 Java 编写的示例代码,该代码实现了在网页端上点击上传附件并选择本地文件进行上传的接口: import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class FileUploadController { @PostMapping("/upload") public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { // 检查文件是否为空 if (file.isEmpty()) { return ResponseEntity.badRequest().body("请选择要上传的文件"); } try { // 执行文件上传操作 // 在这里,你可以实现具体的文件上传逻辑,例如将文件保存到特定位置或将文件存储到数据库中 // 可以使用 file.getInputStream() 获取文件输入流进行操作 return ResponseEntity.ok("文件上传成功"); } catch (Exception e) { return ResponseEntity.status(500).body("文件上传失败"); } } } 以上代码使用 Spring Web 库实现了一个 RESTful 风格的上传文件接口。在这个例子中,/upload 路径对应上传文件的接口。接口

weeq的评论 (共 条)

分享到微博请遵守国家法律