@RestController
@Scope("prototype")
@RequestMapping("/xxxx/xxx/main")
public class JobPicControl { @Autowired
private xxxService xxxService;/**
* @description 上传图片
* @param file
* @param request
* @return
*/
@RequestMapping(value = "/base/xxx/upload", method = RequestMethod.POST)
public String uploadMap(@RequestParam(value = "file", required = false) MultipartFile file,
@RequestParam(value = "xxx", required = false) String xxx, HttpServletRequest request) {
Response response = new Response();
if (file.isEmpty()) {
return "文件上传失败,请重试";
} else {
System.out.println("========================================");
System.out.println("文件长度: " + file.getSize());
System.out.println("文件类型: " + file.getContentType());
System.out.println("文件名称: " + file.getName());
System.out.println("文件原名: " + file.getOriginalFilename());
System.out.println("========================================");
}
String filePath = "/fileLibrary/jobTypePics/";
String realPath = request.getSession().getServletContext().getRealPath(filePath); String fileNewName = xxx+ ".jpg";
System.out.println("fileNewName:"+fileNewName); //保存
try {
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, fileNewName)); } catch (Exception e) {
e.printStackTrace();
return response.failure("更新图失败,请重试").toSimpleResult();
} return "成功"; } /**
* @description 流的方式得到图片
* @param request
* @param httpResponse
*/
@RequestMapping(value = "/base/xxx/getPic", method = RequestMethod.GET)
public void getMapPic1(@RequestParam(value = "iconUrl", required = false) String jobIconUrl,HttpServletRequest request,HttpServletResponse httpResponse) {
String realPath = request.getSession().getServletContext().getRealPath(iconUrl);
String filePath = realPath;
System.out.println("filePath:"+filePath);
File file = new File(filePath);
FileInputStream fis = null;
try { httpResponse.setContentType("image/gif");
OutputStream out = httpResponse.getOutputStream();
fis = new FileInputStream(file);
byte[] b = new byte[fis.available()]; fis.read(b);
out.write(b);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

SpringMVC图片上传与显示的更多相关文章

  1. ruby on rails爬坑(三):图片上传及显示

    一,问题及思路 最近在用rails + react + mysql基本框架写一个cms + client的项目,里面涉及到了图片的上传及显示,下面简单说说思路,至于这个项目的配置部署,应该会在寒假结束 ...

  2. 图片上传即时显示javascript代码

    这是基于javascript的一种图片上传即时显示方法,测试结果IE6和火狐浏览器可以正常使用.google浏览器不兼容. 这种方法兼容性比较差,仅供参考,建议使用ajax方法来即时显示图片. 1.首 ...

  3. 图片上传并显示(兼容ie),图片大小判断

    图片上传并显示(兼容ie),图片大小判断 HTML <div id="swf" style="margin: 0 auto;text-align: center;& ...

  4. struts中用kindeditor实现的图片上传并且显示在页面上

    做公司网站的时候由于需要在内容属性中加入图片,所以就有了这个问题,本来一开始找几篇文章看都是讲修改kindeditor/jsp/file_manager_json.jsp和upload_json.js ...

  5. springmvc图片上传(兼容ie8以上,实时预览)

    html代码: <form id="uploadform" method="post" enctype="multipart/form-data ...

  6. SpringMVC 图片上传,检查图片大小

    使用SpringMVC+Spring 前端提交图片文件到Controller,检查上传图片大小是否符合要求 直接上代码了 1.校验图片大小 这里提供出验证的方法,用于在需要校验的地方调用 /** * ...

  7. ueditor图片上传和显示问题

    图片上传: 这段是contorller代码 @RequestMapping(value = "/uploadImg", method = RequestMethod.POST) @ ...

  8. MVC图片上传并显示缩略图

    前面已经说了怎么通过MVC来上传文件,那么这次就说说如何上传图片然后显示缩略图,这个的实用性还是比较大.用UpLoad文件夹来保存上传的图片,而Temp文件夹来保存缩略图,前面文件上传部分就不再重复了 ...

  9. django 图片上传与显示

    由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...

随机推荐

  1. SQL基础学习_03_数据更新

    数据的插入 1. 基本INSERT语句     INSERT的基本语法为:     INSERT INTO  <表名> (列1, 列2, 列3,  -) VALUES (值1, 值2, 值 ...

  2. PHP动态图像处理

    相关代码见:https://www.github.com/lozybean/learn_www/ 目录 1. 画布管理:     imagecreate():创建一个基于调色板的画布,指定画布的长.宽 ...

  3. mac 安装mysql特种报错的对应解决方式

    参考 :http://www.jianshu.com/p/776e72742c6e 原文废话太多了, 还是看我的好了. 配置环境变量 echo "export PATH=$PATH:/usr ...

  4. H5 + 开发App(分享功能)

    我们开发App有一个不可少的功能,就是分享功能.让用户将app分享到他的社交圈.比如微信 QQ 微博等等. 准备工作:我们要先去申请相关的权限, 这是传送门http://ask.dcloud.net. ...

  5. 多行文字水平垂直居中在div

    <BODY>   <div class="box">    <h3>1.单行文字居中</h3>    <!--设置行高来实现- ...

  6. yield next和yield* next的区别

    yield next和yield* next之间到底有什么区别?为什么需要yield* next?经常会有人提出这个问题.虽然我们在代码中会尽量避免使用yield* next以减少新用户的疑惑,但还是 ...

  7. yum部署、使用 zabbix监控 - 详细过程

    yum部署zabbix监控 第1章 yum部署zabbix服务端... 1 1.1 命令行部署... 1 1.1.1 yum源配置-解释... 2 1.1.2 MariaDB 与 mysql 3 1. ...

  8. Ubuntu Bash and Dash

    http://blog.sina.cn/dpool/blog/s/blog_5f16b0590100r1ub.html?vt=4

  9. 2018年1月 mybatis+ 动态sql连续日期

    2018-01-111 .   如何在项目中快速查找对应的mapper.xml文件 从Controller入手,使用(Ctrl+鼠标左键)找到Service调用的方法,再找到底层方法,就能找到对应ma ...

  10. 如何在一个项目中同时包含mvc建站、webapi接口

    项目做得多了..就会发现有些小项目不想建太多的项目..现在思明在这里和大家分享一下如果再一个项目中同时包含mvc建站以及实现webapi接口 1.新建项目 aps.net web 应用程序 2 新建模 ...