资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用springboot怎么实现图片上传和下载功能

今天就跟大家聊聊有关使用spring boot怎么实现图片上传和下载功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。成都创新互联专业提供网站设计制作、成都网站建设,网页设计,网站制作(企业站、响应式网站、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!

1、核心的controller代码

package com.qwrt.station.websocket.controller; 
 
import com.alibaba.fastjson.JSONObject; 
import com.qwrt.station.common.util.JsonUtil; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.multipart.MultipartFile; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.*; 
 
/** 
 * Created by jack on 2017/10/30. 
 */ 
@RestController 
@RequestMapping("v1/uploadDownload") 
public class UploadDownloadController { 
 private static final Logger logger = LoggerFactory.getLogger(UploadDownloadController.class); 
 @Value("${uploadDir}") 
 private String uploadDir; 
 
 @RequestMapping(value = "/uploadImage", method = RequestMethod.POST) 
 public JSONObject uploadImage(@RequestParam(value = "file") MultipartFile file) throws RuntimeException { 
 if (file.isEmpty()) { 
  return JsonUtil.getFailJsonObject("文件不能为空"); 
 } 
 // 获取文件名 
 String fileName = file.getOriginalFilename(); 
 logger.info("上传的文件名为:" + fileName); 
 // 获取文件的后缀名 
 String suffixName = fileName.substring(fileName.lastIndexOf(".")); 
 logger.info("上传的后缀名为:" + suffixName); 
 // 文件上传后的路径 
 String filePath = uploadDir; 
 // 解决中文问题,liunx下中文路径,图片显示问题 
 // fileName = UUID.randomUUID() + suffixName; 
 File dest = new File(filePath + fileName); 
 // 检测是否存在目录 
 if (!dest.getParentFile().exists()) { 
  dest.getParentFile().mkdirs(); 
 } 
 try { 
  file.transferTo(dest); 
  logger.info("上传成功后的文件路径未:" + filePath + fileName); 
  return JsonUtil.getSuccessJsonObject(fileName); 
 } catch (IllegalStateException e) { 
  e.printStackTrace(); 
 } catch (IOException e) { 
  e.printStackTrace(); 
 } 
 return JsonUtil.getFailJsonObject("文件上传失败"); 
 } 
 
 //文件下载相关代码 
 @RequestMapping(value = "/downloadImage",method = RequestMethod.GET) 
 public String downloadImage(String imageName,HttpServletRequest request, HttpServletResponse response) { 
 //String fileName = "123.JPG"; 
 logger.debug("the imageName is : "+imageName); 
 String fileUrl = uploadDir+imageName; 
 if (fileUrl != null) { 
  //当前是从该工程的WEB-INF//File//下获取文件(该目录可以在下面一行代码配置)然后下载到C:\\users\\downloads即本机的默认下载的目录 
  /* String realPath = request.getServletContext().getRealPath( 
   "//WEB-INF//");*/ 
  /*File file = new File(realPath, fileName);*/ 
  File file = new File(fileUrl); 
  if (file.exists()) { 
  response.setContentType("application/force-download");// 设置强制下载不打开 
  response.addHeader("Content-Disposition", 
   "attachment;fileName=" + imageName);// 设置文件名 
  byte[] buffer = new byte[1024]; 
  FileInputStream fis = null; 
  BufferedInputStream bis = null; 
  try { 
   fis = new FileInputStream(file); 
   bis = new BufferedInputStream(fis); 
   OutputStream os = response.getOutputStream(); 
   int i = bis.read(buffer); 
   while (i != -1) { 
   os.write(buffer, 0, i); 
   i = bis.read(buffer); 
   } 
   System.out.println("success"); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } finally { 
   if (bis != null) { 
   try { 
    bis.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   } 
   if (fis != null) { 
   try { 
    fis.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   } 
  } 
  } 
 } 
 return null; 
 } 
 
 
}

     上面的代码有两个方法,上面的方法是图片上传的方法,下面的方法是图片下载的方法。下载图片需要传入图片的文件名,在ios,android手机,谷歌浏览器测试,上传下载没有问题。

2、测试的html的核心代码如下,进行图片的上传和下载:

 
 
 
  
  
 websocket chat 
  
 
  
 
 
        发送消息    websocket连接    断开websocket连接                           
              
               
                
                        

上面的代码有些和图片的上传和下载没有关系,根据需要自己去掉,看图片上传和下载的核心代码,需要引入jquery。

3、spring boot的属性配置:

1.解决图片上传太大的问题:

spring:
 http:
 multipart:
 max-file-size: 100Mb #文件上传大小
 max-request-size: 200Mb #最打请求大小

这是新版spring boot解决图片或者文件上传太大的问题,老板的不是这样解决的。可以自己查资料

2.配置文件上传保存的位置:

#上传位置
uploadDir: F:\mystudy\pic\

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

看完上述内容,你们对使用spring boot怎么实现图片上传和下载功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


文章题目:使用springboot怎么实现图片上传和下载功能
网站网址:http://cdkjz.cn/article/phoiig.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220