Java web项目 上传图片保存到数据库,并且查看图片,(从eclipse上移动到tomact服务器上,之路径更改,包括显示图片和导出excel)
//项目做完之后,在本机电脑运行完全正常,上传图片,显示图片,导出excel,读取excel等功能,没有任何问题,但是,当打成war包放到服务器上时,这些功能全部不能正常使用。
最大的原因就是,本机测试跟服务器上的路径发生了变化。
记录一下,上传图片和显示图片的代码
1.前端页面:
<form action="${pageContext.request.contextPath}/UploadWeiXiuServlet"
enctype="multipart/form-data" method="post">
<input type="file" name="file" multiple="multiple" align="center">
<input type="submit" value="提交" />
</form>
2:后台servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("上传维修图片附件的servlet");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String path = request.getSession().getServletContext().getRealPath(
"/upload/weixiuimg");
File filemulu =new File(path);
//如果文件夹不存在则创建
if (!filemulu .exists())
{
System.out.println("//不存在");
filemulu .mkdir();
} else
{
System.out.println("//目录存在");
}
log.info("路径:"+path);
Map<String, String> map = Upload.upload(request, 1024 * 1024 * 10, path);
String file= map.get("file"); // 名称
// String newFile = map.get("newFile");// 地址
MuJUService mjService = new MuJUService();
//System.out.println(map.get("type"));
boolean flag=mjService.uploadImg("upload/weixiuimg/"+file,map.get("wx_id"));//调用方法,存到数据库
HttpSession session=request.getSession();
if (flag) {
log.info("图片上传成功");
session.setAttribute("flag", "上传成功");
}else {
log.info("图片上传失败");
session.setAttribute("flag", "上传失败");
}
3.upload类
public static Map<String, String> upload(HttpServletRequest request,
int maxSize, String path) {
//以map形式保存数据 key对应保存的是获取界面上的name名称 value保存的是获取界面上的name对应的值
Map<String, String> map = new HashMap<String, String>();
Part part = null;
try {
MultipartParser mrequest = new MultipartParser(request, maxSize);
mrequest.setEncoding("utf-8");
//遍历所有的part组
while ((part = mrequest.readNextPart()) != null) {
if (part.isFile()) { //判断是否是文件
FilePart filepart = (FilePart) part;//转化成文件组
String fileName = filepart.getFileName();//得到文件名
if (fileName != null && fileName.length() > 0) {
// 取得扩展名
String fileExtName = fileName.substring(
fileName.lastIndexOf(".") + 1).toLowerCase();
// 只上传图片 //判断图片上传的格式是否符合 后缀名是否有效
if (fileExtName.equalsIgnoreCase("jpeg")
|| fileExtName.equalsIgnoreCase("png")||
fileExtName.equalsIgnoreCase("jpg")
|| fileExtName.equalsIgnoreCase("gif")
|| fileExtName.equalsIgnoreCase("ico")
|| fileExtName.equalsIgnoreCase("bmp")
|| fileExtName.equalsIgnoreCase("flv")
|| fileExtName.equalsIgnoreCase("mp4")
|| fileExtName.equalsIgnoreCase("mp3")) { /*String newFileName = new Date().getTime() + "."+ fileExtName;//重新改文件名 文件名+扩展名 */
String newFileName =new Date().getTime() +fileName;//不改图片名字
String newPath = path + "/" + newFileName; //文件处理文件上传的路径
File newFile = new File(newPath);
filepart.writeTo(newFile); //将文件真正写入到对应的文件夹中
//filepart.getName() 得到 request 要接收的参数的名字
map.put(filepart.getName(), newFileName);//把文件信息保存到map中
map.put("newFile", newFile.toString());
} else {
map.put("geshi", "geshi");
continue;
}// 说明上传的不是图片
} else {
map.put("yes","yes");
continue; // 说明没有选择上传图片
}
} else if (part.isParam()) { //判断是否是参数
ParamPart paramPart = (ParamPart) part;
map.put(paramPart.getName(), paramPart.getStringValue());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
4.显示图片,前端 jquery----弹框bootstrap,模态框传值
////request.getScheme()得到的:http:loaclhost:8888/
在eclipse中测试的时候可能只要得到 String path =request.getContextPath();---/muju_pro(项目名),在拼接上数据库中图片的url就可以取到了,
但是在服务器上必须是Http:10.1.10.114:8888.。。。。这样的路径。
<% String path =request.getContextPath();
String realpath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> //绑定模态框展示的方法
$('#portrait1').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // 触发事件的按钮
var recipient = button.data('whatever') // 解析出whatever内容
var modal = $(this) //获得模态框本身
//更改将title的text
// alert("C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/muju_pro/"+recipient);
/* modal.find('.modal-body img').attr("src",recipient); */
modal.find('.modal-body img').attr("src","<%=realpath%>/"+recipient); })
html
<div class="modal fade" id="addSource" role="dialog" aria-labelledby="gridSystemModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="gridSystemModalLabel">修改图片</h4> </div>
<div class="modal-body">
<div class="container-fluid">
<form class="form-horizontal" action="${pageContext.request.contextPath}/UploadWeiXiuServlet"
enctype="multipart/form-data" method="post"><!-- -->
<div class="form-group">
<label for="sLink" class="col-xs-3 control-label">上传图片:</label>
<div>
<input type="hidden" id="wx_id2" name="wx_id"/>
<input type="hidden" value="xiugai" name="type"/>
<input type="file" name="file" multiple="multiple" align="center">
<input type="submit" value="提交" />
</div>
<div class="col-xs-8 ">
<input type="hidden" id="wx_id" name="wx_id">
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-xs btn-xs btn-white" data-dismiss="modal">取消下单</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
Java web项目 上传图片保存到数据库,并且查看图片,(从eclipse上移动到tomact服务器上,之路径更改,包括显示图片和导出excel)的更多相关文章
- Java Web项目中连接Access数据库的配置方法
本文是对前几天的"JDBC连接Access数据库的几种方式"这篇的升级.因为在做一些小项目的时候遇到的问题,因此才决定写这篇博客的.昨天已经将博客公布了.可是后来经过一些验证有点问 ...
- Java web项目 Jxl 读取excel 并保存到数据库,(从eclipse上移动到tomact服务器上,之路径更改,)
最开始在eclipse中测试的时候,并没有上传到服务器上,后来发现,想要读取数据必须上传服务器然后把文件删除就可以了,服务器不可以直接读取外地的文件.用到jxl 1.上传到服务器 前端 <for ...
- Java web项目JXl导出excel,(从eclipse上移动到tomact服务器上,之路径更改)
我用的是jxl导出excel,比较简单,最开始我是固定路径不能选择,很局限,后来改了,而且固定路径当把项目放在服务器上时,路径不可行. 在网上各位大神的帮助成功设置响应头,并且可选保存路径. 1.前端 ...
- SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。
熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...
- Mac下Intellij IDea发布Java Web项目详解五 开始测试
测试前准备工作目录 Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intell ...
- 如何使用域名访问自己的Windows服务器(Java web 项目)
如何使用域名访问自己的Windows服务器(Java web 项目) 写在前面 前段时间在阿里云弄了个学生服务器,就想着自己搭建一个网站试一试,在网上查阅相关资料时发现大部分都是基于服务器是Linux ...
- 初次Java web项目的建立以及与数据库的连接
题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...
- 阿里云部署Java web项目初体验(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在 ...
- Java Web项目 配置 ueditor心得
近期的JAVA项目,由于客户要求需要引入富文本编辑器. 参考了两款插件,一款是ckeditor,一款是ueditor. ckeditor在上传文件的时候必须配合ckfinder使用,而ckfinder ...
随机推荐
- nfs4使用中的防火墙配置
一,查看本地centos的版本: [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ...
- centos8环境安装配置rsync
一,查看本地centos的版本: [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ...
- Go go.mod入门
什么是go.mod? Go.mod是Golang1.11版本新引入的官方包管理工具用于解决之前没有地方记录依赖包具体版本的问题,方便依赖包的管理. Go.mod其实就是一个Modules,关于Modu ...
- 构建调试Linux内核网络的环境Menuos系统
一.Linux内核源码下载 下载网址为:https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.1.tar.xz 下载完成后放入home/Menu ...
- 圆形进度条的模仿1-DrawArc,DrawCircle,DrawText讲解
1:画弧 canvas.drawArc(oval,startAngle,sweepAngle,useCenter,paint) 第一个参数:绘制的区域,oval可以是被定好了的一个区域,也可以将ova ...
- 超简单集成HMS ML Kit文字超分能力,一键提升文本分辨率
前言 大家有没有遇到过这种情况,在浏览微博或者公众号时看到一段有趣的文字,于是截图发到朋友圈想和好友分享.但是在发布图片时,软件会对图片强制进行压缩,导致图片分辨率下降,文字变得模糊难以阅读.那么有没 ...
- C#Socket通讯(1)
前言 因为自己需要开发一款自己的游戏,因为设计网络方面,所以我找了很多的资料,再加上考虑到Unity游戏客户端直接连接数据库很容易导致数据库泄露,再加上网上很多的服务端都是用控制台做的,所以我就想做个 ...
- frida框架hook获取方法输出参数(常用于简单的so输出参数获取,快速开发)
一.模板 function douyinencode(data) { var result = {}; Java.perform(function () { try { var Test = Java ...
- SWPUCTF_2019_p1KkHeap
SWPUCTF_2019_p1KkHeap 环境:ubuntu18 考点:UAF,沙箱逃逸 ubuntu18现在不能构造double free了!!! 所以我用patchelf来做 IDA逆一下 可以 ...
- Eureka实现注册中心
CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼.它是分布 ...