springboot项目图片上传,回显;使用外部静态资源路径回显图片

//前端图片是Base64字符串形式传递图片参数;需要用Base解密,写入到本地磁盘中
public String upload(String string){

	解析图片(Base64):
	response.setHeader("Access-Control-Allow-Origin","*");  // 第二个参数填写允许跨域的域名称,不建议直接写 "*"
	response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
	response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");

	// 接收跨域的cookie
	response.setHeader("Access-Control-Allow-Credentials", "true");
	url = url.substring(url.indexOf(",")+1,url.length());
	String imagePath = null;
	BASE64Decoder decoder = new BASE64Decoder();
	String strName = null;
	String string2 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
	System.out.println(string2);
	try {
		byte[] bytes = decoder.decodeBuffer(url);
		// 处理数据
		for (int i = 0; i < bytes.length; ++i) {
			if (bytes[i] < 0) {
				bytes[i] += 256;
			}
		}
		strName = UUID.randomUUID().toString();
		System.out.println(strName);
		imagePath = "E:/javaworkspace/project/rbApi/image/"+strName+".png";
		OutputStream out = new FileOutputStream(imagePath);
		out.write(bytes);
		out.flush();
		out.close();
	}catch (Exception e){
		e.printStackTrace();
	}
}
//springboot项目
//以@RequestParam("url") List<MultipartFile> url接收上传图片;写入到本地磁盘中
//返回路径是磁盘路径,将磁盘路径映射到外部静态资源;访问:项目路径+映射路径+文件名
@RequestMapping(value = "/uploadImage")
@ResponseBody
public PictureUrl upload(@RequestParam("url") List<MultipartFile> url, HttpServletRequest request, HttpServletResponse response){
	PictureUrl pictureUrl = new PictureUrl();
	String contentPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
	try{
		if(url.size() == 0){
			pictureUrl.setStatusText("false");
		}else{
			OutputStream os = null;
			List<String> urlList = new ArrayList<>();
			for (MultipartFile file : url){
				String fileName = file.getOriginalFilename();
				String fileNam2 = UUID.randomUUID().toString();
				String imagePath = "E:/javaworkspace/project/rbApi/image/"+fileNam2+".png";
				File file1 = new File(imagePath);
				os = new FileOutputStream(file1);
				os.write(file.getBytes());
				//String imagePath2 = contentPath + "/showImage?id=" + fileNam2+".png";
				//String imagePath2 = upload + fileNam2+".png";
				String imagePath2 = contentPath+"/image/"+fileNam2+".png";
				urlList.add(imagePath2);
				System.out.println("文件路径:"+imagePath2);
			}
			pictureUrl.setStatusText("ok");
			pictureUrl.setImageUrl(urlList);
			os.flush();
			os.close();
		}

	}catch (Exception e){
		e.printStackTrace();
		pictureUrl.setStatusText("false");
		return pictureUrl;
	}

	return pictureUrl;

}
//将图片以二进制的形式输出,前端路径可为:(项目路径+方法+文件名)
 /**
 * 回显图片
 * @param id
 * @param response
 * @return
 */
@RequestMapping("/showImage")
@ResponseBody
public AjaxResult download(String id, HttpServletResponse response){
	try{
		response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
		String path ="E:/javaworkspace/project/rbApi/image/"+id;
		File file = new File(path);//括号里参数为文件图片路径
		if(file.exists()){   //如果文件存在
			InputStream in = new FileInputStream(path);   //用该文件创建一个输入流
			OutputStream os = response.getOutputStream();  //创建输出流
			byte[] b = new byte[1024];
			while( in.read(b)!= -1){
				os.write(b);
			}
			in.close();
			os.flush();
			os.close();
		}
		return null;
	}catch (Exception e){
		e.printStackTrace();
		return AjaxResult.build("false",null );
	}
}

//将磁盘文件路径映射为项目访问路径
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**").addResourceLocations("file:E:/javaworkspace/project/rbApi/image/");
    }
}

  得到

原文地址:https://www.cnblogs.com/lazyli/p/11126945.html

时间: 2024-08-10 02:18:05

springboot项目图片上传,回显;使用外部静态资源路径回显图片的相关文章

html5图片上传时IOS和Android均显示摄像头拍照和图片选择

最近在做信开发时,发现<input type="file" />在IOS中可以拍照或从照片图库选择,而Android系统则显示资源管理器,无拍照选项,网上查找资料,改为<input type="file" capture="camera">后,Android可显示相机和文档,但IOS则只有拍照选项了,最后通过判断设备类型使在IOS和Android下均可以显示拍照和图库选择,代码如下: var u = navigator.u

springboot+web文件上传和下载

一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocatio

使用百度UMeditor富文本编辑器,修改自定义图片上传,修改源码

富文本编辑器,不多说了,这个大家应该都用到过,至于用到的什么版本,那就分很多种 CKEditor:很早以前叫FCK,那个时候也用过,现在改名了,比较流行的一个插件,国外很多公司在用 UEDITOR:百度开发的插件,lite版是UM EasyUI编辑器:用easyUI的都懂,基本上肯定用到 其他的富文本编辑器就不说了,前两个小编我用的比较多 本来我是比较倾向于CKEditor,但是这个插件不支持图片上传,图片功能只能链接过去,而不能在控件本身中上传,所以就选择了UMeditor 为啥选择UM,不选

5行代码实现微信小程序图片上传与腾讯免费5G存储空间的使用

本文介绍了如何在微信小程序开发中使用腾讯官方提供的云开发功能快速实现图片的上传与存储,以及介绍云开发的 5G 存储空间的基本使用方法,这将大大提高微信小程序的开发效率,同时也是微信小程序系列教程的视频版更新的文字版本摘要. 此文为 「60 节实战课微信小程序开发视频教程」 的第 51 小节内容,如果需要查看视频版本的实战操作,请直接跳至文章的最后部分查看. 1.云开发图片空间简介 在之前的文章 微信小程序开发平台新功能「云开发」快速上手体验 中我们简要介绍了腾讯官方给所有的微信小程序开发提供的云

分布式系统图片上传方案

思路分析 直接将图片上传到一个指定的目录,访问.下载图片都访问这个目录. 由于项目最终是要部署到Linux环境,所以直接将图片上传到Linux服务器. 问题:那如何将图片上传到Linux呢? 答:使用vsftpd组件,实现文件传输. vsftpd简介 问题1:vsftpd是什么? 答:ftp(File Transfer Protocol)文件传输协议.(实现不同操作系统之间文件的传输) vsftpd是一个基于ftp协议的文件传输服务器软件. 问题2:vsftpd作用是什么? 答:传输文件.(跨平

Django(十九)文件上传:图片上传(后台上传、自定义上传)、

一.基本设置 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/http/file-uploads/ 1)配置project1/settings.py 因为图片也属于静态文件,所以保存到static目录下. MEDIA_ROOT=os.path.join(BASE_DIR,"static/media") 2)在static目录下创建media目录,再创建应用名称的目录,此例为app1 F:\Test\django-demo\pro

ckeditor4.5.1配置图片上传的方法

本篇博文主要面向初学者,一步一步地实现ckeditor的图片上传,欢迎各位大神指正. ckeditor的图片上传默认是关闭的,网上也有很多相关的教程,可是不是讲的不完整,就是版本太旧已经不适用.我写这篇博文主要面向初学者,有错误的地方欢迎指正. 截止本文撰写,ckeditor最新版本是4.5.1,我下载的版本是4.5.1standard,如果本文的方法不管用,请下载同一版本尝试. 1.开启图片上传界面 文件地址:ckeditor/plugins/image/dialogs/image.js 搜索

妈蛋:kinMaxShow轮播图异常,WebUploader图片上传坑爹,图片被压缩了

今天晚上在改造轮播图. 原来的代码是这样的: <div> <img src="${static}/image/index/banner/`.jpg" /> </div> <div> <img src="${static}/image/index/banner/2.jpg" /> </div> <div> <img src="${static}/image/index/

妈蛋:kinMaxShow旋转木马异常,WebUploader图片上传坑爹,图像被压缩

今天晚上在改造轮播图. 原来的代码是这种: <div> <img src="${static}/image/index/banner/`.jpg" /> </div> <div> <img src="${static}/image/index/banner/2.jpg" /> </div> <div> <img src="${static}/image/index/b