//项目做完之后,在本机电脑运行完全正常,上传图片,显示图片,导出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">&times;</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)的更多相关文章

  1. Java Web项目中连接Access数据库的配置方法

    本文是对前几天的"JDBC连接Access数据库的几种方式"这篇的升级.因为在做一些小项目的时候遇到的问题,因此才决定写这篇博客的.昨天已经将博客公布了.可是后来经过一些验证有点问 ...

  2. Java web项目 Jxl 读取excel 并保存到数据库,(从eclipse上移动到tomact服务器上,之路径更改,)

    最开始在eclipse中测试的时候,并没有上传到服务器上,后来发现,想要读取数据必须上传服务器然后把文件删除就可以了,服务器不可以直接读取外地的文件.用到jxl 1.上传到服务器 前端 <for ...

  3. Java web项目JXl导出excel,(从eclipse上移动到tomact服务器上,之路径更改)

    我用的是jxl导出excel,比较简单,最开始我是固定路径不能选择,很局限,后来改了,而且固定路径当把项目放在服务器上时,路径不可行. 在网上各位大神的帮助成功设置响应头,并且可选保存路径. 1.前端 ...

  4. SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。

    熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...

  5. Mac下Intellij IDea发布Java Web项目详解五 开始测试

    测试前准备工作目录 Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intell ...

  6. 如何使用域名访问自己的Windows服务器(Java web 项目)

    如何使用域名访问自己的Windows服务器(Java web 项目) 写在前面 前段时间在阿里云弄了个学生服务器,就想着自己搭建一个网站试一试,在网上查阅相关资料时发现大部分都是基于服务器是Linux ...

  7. 初次Java web项目的建立以及与数据库的连接

    题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...

  8. 阿里云部署Java web项目初体验(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在 ...

  9. Java Web项目 配置 ueditor心得

    近期的JAVA项目,由于客户要求需要引入富文本编辑器. 参考了两款插件,一款是ckeditor,一款是ueditor. ckeditor在上传文件的时候必须配合ckfinder使用,而ckfinder ...

随机推荐

  1. 免费申请HTTPS通配符证书

    前言 在阿里云买了一个域名giantliu.cn 部署了自己的博客系统 https://www.giantliu.cn/ 所有用https证书是Let's Encrypt免费申请的 因为申请的免费证书 ...

  2. pycharm pro2020版专业版永久激活

    pycharm2020版本专业版永久激活[亲测有效] pycharm2020.1版安装包与破解工具下载 可私信我获取资源. 公众号,轻松学编程 教程 1.先下载安装包和破解补丁压缩包,然后点击pych ...

  3. 【Jmeter】Jmeter安装配置教程

    jmeter安装配置教程 1.安装jdk,配置环境变量 进入官网,https://www.oracle.com/downloads/index.html#java,选择 Java (JDK) for ...

  4. 【漏洞复现】Shiro<=1.2.4反序列化漏洞

    0x01 概述 Shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从 ...

  5. JS中使用for-each遍历数组

    1 let array = [1, 3, 6, 8, 9, 0, 5]; 2 /* 3 index是数组索引 4 value代表数组的值 5 arr是指整个数组 6 */ 7 array.forEac ...

  6. How to using code post packingSlip on Quality Orders Form[AX2009]

    For simple user operation posting packing slip with purchase order. we added a function button on Qu ...

  7. 论文解读 - Composition Based Multi Relational Graph Convolutional Networks

    1 简介 随着图卷积神经网络在近年来的不断发展,其对于图结构数据的建模能力愈发强大.然而现阶段的工作大多针对简单无向图或者异质图的表示学习,对图中边存在方向和类型的特殊图----多关系图(Multi- ...

  8. 3.3 Spring5源码---循环依赖过程中spring读取不完整bean的最终解决方案

    根据之前解析的循环依赖的源码, 分析了一级缓存,二级缓存,三级缓存的作用以及如何解决循环依赖的. 然而在多线程的情况下, Spring在创建bean的过程中, 可能会读取到不完整的bean. 下面, ...

  9. 使用arm-none-eabi-gdb报错error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

    使用arm-none-eabi-gdb报错error while loading shared libraries: libncurses.so.5: cannot open shared objec ...

  10. 内网渗透 day1-基础

    粘滞键和放大镜 1. 到/windows/system32下用takeown改变该文件夹下的文件拥有权(因为粘滞键和放大镜都在system32文件夹下) cd /Windows/system32    ...