欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > springboot上传下载文件

springboot上传下载文件

2025/2/25 18:57:31 来源:https://blog.csdn.net/qq_37398465/article/details/143869762  浏览:    关键词:springboot上传下载文件

@RequestMapping(“bigJson”)
@RestController
@Slf4j
public class TestBigJsonController {

@Resource
private BigjsonService bigjsonService;@PostMapping("uploadJsonFile")
public ResponseResult<Long> uploadJsonFile(@RequestParam("file")MultipartFile file){if (file.isEmpty()) {return ResponseResult.error();}try {Bigjson bigjson = new Bigjson();bigjson.setJsonFileName(file.getName());// 解析JSON文件ObjectMapper objectMapper = new ObjectMapper();JSONObject jsonObject = objectMapper.readValue(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8), JSONObject.class);bigjson.setJsonFile(jsonObject.toJSONString());//Long id = bigjsonService.uploadJsonFile(bigjson);// 返回成功响应return ResponseResult.success(id);} catch (IOException e) {log.error("上传错误");return ResponseResult.error();}
}@GetMapping("/downloadJson")
public ResponseEntity<byte[]> downloadJson(@RequestParam("id") Long id){Bigjson bigjson = bigjsonService.getById(id);byte[] bytes = bigjson.getJsonFile().getBytes();// 设置响应头HttpHeaders headers = new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+bigjson.getJsonFileName()+".json");headers.add("Cache-Control", "no-cache, no-store, must-revalidate");headers.add("Pragma", "no-cache");headers.add("Expires", "0");// 返回响应return new ResponseEntity<>(bytes, headers, HttpStatus.OK);}

}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com