SpringMVC 框架完成图片上传到项目路径操作
/**
* 保存添加
*
* @return
*/
@RequestMapping(value = "taizhang/add.action", method = { RequestMethod.POST })
public String taizhangAdd(Equipment equipment, @RequestParam("photo") MultipartFile file,
HttpServletRequest request) {
// 添加图片
// 1.判断头像是否为空
String n = null;
if (file != null || file.equals("")) {
// 文件上传处理
// 1.获取上传路径
String str = request.getServletContext().getRealPath("//upload//equipViews");
// 2.如果该路径不存在,创建此路径
File path = new File(str);
if (!path.exists()) {
path.mkdirs();
}
// 3.获取名字 拼接路径
String name = file.getOriginalFilename();
n = name;
// 根据是否有name来判断是否有图片
/*
* if(name.equals("")){
*
* }
*/
// 4.对名字进行处理
name = UUID.randomUUID() + name;
// 5.拼接路径
File desPath = new File(path, name);
// 6.文件上传
try {
// writeByteArrayToFile(要上传的路径, 文件---字节数组)
FileUtils.writeByteArrayToFile(desPath, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 7.把路径放到数据库里 把该路径设置到user里面
equipment.setPic("equipViews//" + name);
}
/**
* 如果文件上传了,修改数据库 把原图片給替代 如果文件沒有上传 1.把原来的给他 2.不修改图片的路径 没有 --改 没有值 --不改 有值
* --不改
*/
if (n == null || n.equals("")) {
equipment.setPic(null);
}
equipmentService.insertSelective(equipment);
return "redirect:/equipViews/taizhang.action";
}
// 跳转到查看详情页面
@RequestMapping("taizhang/fingById.action")
public String taizhangFingById(Model model, Integer id) {
Equipment equipment = equipmentService.selectByPrimaryKey(id);
model.addAttribute("equipmentList", equipment);
return "equipViews/detailsParameter";
}
SpringMVC 框架完成图片上传到项目路径操作的更多相关文章
- springMVC和ckeditor图片上传
springMVC和ckeditor图片上传 http://blog.csdn.net/liuchangqing123/article/details/45270977 修正一下路径问题: packa ...
- 使用SpringMVC框架实现文件上传和下载功能
使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!—配置文件上传解释器 --> <mvc:annotation-driven>&l ...
- ssm框架实现图片上传显示并保存地址到数据库
本案例是通过springmvc+spring+mybatis框架以商品上传为例,实现的图片上传功能,并把图片的地址保存到数据库并在前台显示上传的图片. 本项目是使用maven搭建的项目,首先看下项目结 ...
- SpringMVC框架——文件的上传与下载
使用SpringMVC框架做个小练习,需求: 1.单个图片上传并显示到页面中: 2.多个图片上传并显示到页面中: 3.上传文件后下载文件: 1.pom.xml中添加依赖 <!-- 文件上传 -- ...
- Ueditor1.4.3.3+springMvc+maven 实现图片上传
前记:由于项目中需要有一个新增数据并且要能支持表格图片上传的功能.使用了ueditor控件.为实现这个功能,从一开始什么都看不懂,到一直连着弄了5天,总算是有了眉目.在此记录一下以便能帮到可以和我一样 ...
- SpringMVC框架06——文件上传与下载
1.文件上传 Spring MVC框架的文件上传是基于commons-fileupload组件的文件上传,只不过Spring MVC框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现. ...
- Linux搭建GIT 使用Eclipse创建并上传Git项目 EGit操作
Linux搭建Git 1. gitblit服务器文档 http://gitblit.com/setup_go.html 2. 安装jdk 参考 http://blog.csdn.net/jerome_ ...
- Angular4图片上传预览路径不安全问题
在Angular4中,通过input:file上传选择图片本地预览的时候,通过window.URL.createObjectURL获取的url赋值给image的src出现错误: WARNING: sa ...
- springMVC框架下——通用接口之图片上传接口
我所想要的图片上传接口是指服务器端在完成图片上传后,返回一个可访问的图片地址. spring mvc框架下图片上传非常简单,如下 @RequestMapping(value="/upload ...
随机推荐
- Flask图书管管理表
后端的读写 from flask import Flask,render_template,request from flask_sqlalchemy import SQLAlchemy #导入时间模 ...
- 什么是openstack
什么是openstack OpenStack是一个云平台管理的项目,它不是一个软件. 这个项目由几个主要的组件组合起来完成一些具体的工作. 想要了解openstack,第一步我们可以观察他的概念图: ...
- flask 连接数据库
FLASK 连接mysql 数据库 1 # -*- encoding: utf-8 -*- 2 3 from flask import Flask 4 #导入第三方连接库 5 from flask_s ...
- 接口转换 数据库列表的内容 显示在datagrid
public class AddressConverter : IValueConverter { public object Convert(object value, Type targetTyp ...
- [转] ElasticSearch 常用的查询过滤语句
备忘remark https://www.cnblogs.com/ghj1976/p/5293250.html query 和 filter 的区别请看: http://www.cnblogs.co ...
- libevent的入门学习-库的安装【转】
转自:https://blog.csdn.net/lookintosky/article/details/61658067 libevent的入门学习-库的安装最近开始接触Linux应用层的东西,发现 ...
- C++学习day1
1. 有符号整数 对于有符号整数,最高位为0表示正数,为1表示负数 负数的绝对值为除最高位外,其余取反再加1 int 字节数:4,取值范围:-232~232-1 最大值为232-1 最小值即为 000 ...
- 【转】C++标准转换运算符reinterpret_cast
reinterpret_cast<new_type> (expression) reinterpret_cast运算符是用来处理无关类型之间的转换:它会产生一个新的值,这个值会有与原始参数 ...
- html5 - history 历史管理
参考文章: w3c : http://www.w3.org/html/ig/zh/wiki/HTML5/history 张鑫旭 : http://www.zhangxinxu.com/wo ...
- This project is not a myeclipse hibernate project . Assuming Hibernate 3 capabilities configuration editor
开某工程Hibernate配置文件时出现提示,信息大概如标题. 根据网友提供,选中工程,点击MyEclipse-->Project capabilities-->add hibernate ...