使用springboot简单实现下载图片的功能

本文介绍了如何使用SpringBoot来实现一个简单的图片下载功能。在后台配置中,通过设置静态资源路径和端口,使得系统能够根据前端提供的图片路径找到对应的文件。在前台,用户可以通过访问特定URL上传图片,并在页面上预览,点击图片可以触发下载,用户可以选择下载路径完成文件下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用springboot简单实现下载图片的功能

后台

@Controller
public class IndexController {
   
   
    @Value("${my-config.file-path}")
    private String myFilePath;

    @RequestMapping("test")
    public String test() {
   
   
        return "index";
    }

    @RequestMapping("upload")
    public String upload(@RequestParam("file") MultipartFile file, Model model, HttpServletRequest req) {
   
   
        try {
   
   
            String fileName = System.currentTimeMillis()+file.getOriginalFilename();
            String destFileName=myFilePath+"uploaded"+ File.separator+fileName;

            File destFile = new File(destFileName);
            if (!destFile.getParentFile().exists()) {
   
   
                destFile.getParentFile().mkdirs();
            }
            file.transferTo(destFile);
            model.addAttribute("filename","uploaded/"+fileName);
        } catch (FileNotFoundException e) {
   
   
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        } catch (IOException e) {
   
   
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        }
        return "index";
    }

    @ResponseBody
    @GetMapping("/download")
    public String downloadImage(@RequestParam(value = "imageName",required = false) String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
   
   

        Stri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值