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 ...
随机推荐
- swoole 协程介绍
协程的执行顺序: 1 2 3 4 5 6 7 8 9 go(function () { echo "hello go1 \n"; }); echo "hell ...
- C# URL编码
#region URL编码 /// <summary> /// URL编码 /// </summary> /// <param name="str"& ...
- Python基础知识点整理(详细)
Python知识点整理(详细) 输出函数 print()可以向屏幕打印内容,或者在打开指定文件后,向文件中输入内容 输入函数 input([prompt])[prompt] 为输入的提示字符.该函数返 ...
- git删除缓存区中文件
删除缓冲区中的文件 git rm --cached "文件路径",不删除物理文件,仅将该文件从缓存中删除: git rm --f "文件路径",不仅将该文件从缓 ...
- JS里各种类型的循环
for... for( 初始条件; 判断条件; 递增条件 ) { ... } for ... in 可以把一个对象里面的所有属性依次循环出来 var person = { name: 'Jack', ...
- 一分钟了解"秒杀"系统
关于秒杀,第一反应都是实现起来比较复杂.难点在于:并发读+并发写+设计兜底方案的实现. 比如QQ,虽然数据量很大,但是多数的数据都是细粒度的数据查询,锁冲突比较少:但12306涉及到大量的读写操作,对 ...
- 通过maven创建springboot项目
1,idea选择创建一个maven项目 2,pom.xml <dependencies> <dependency> <groupId>org.springframe ...
- 拿了十几个offer,怎样做选择?
本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 最近收到好几个读者的咨询,关于如何选offer的 ...
- 2020-2021-1 20209306 《linux内核原理与分析》第三周作业
目录 一.实验:完成一个简单的时间片轮转多道程序内核代码 二.学习总结 1.堆栈相关的寄存器 2.堆栈操作 3.其他关键寄存器 4.用堆栈来传递函数的参数 5.了解了函数如何传递返回值,堆栈还提供局部 ...
- 非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!
Zuul是spring cloud中的微服务网关.网关:是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也是 ...