//项目做完之后,在本机电脑运行完全正常,上传图片,显示图片,导出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. spring boot:使用caffeine+redis做二级缓存(spring boot 2.3.1)

    一,为什么要使用二级缓存? 我们通常会使用caffeine做本地缓存(或者叫做进程内缓存), 它的优点是速度快,操作方便,缺点是不方便管理,不方便扩展 而通常会使用redis作为分布式缓存, 它的优点 ...

  2. buuctf-misc-[BJDCTF 2nd]圣火昭昭-y1ng 1

    开局一张图片,flag全靠猜,那这个是不是和outguess工具有关呢?于是我们显示查看了图片的详细信息 看到是新佛曰,于是我们用新佛曰论禅解密:http://hi.pcmoe.net/buddha. ...

  3. if当中是赋值怎么办

    1.Java中,赋值是有返回值的 ,赋什么值,就返回什么值.比如这题,x=y,返回y的值,所以括号里的值是1. 2.Java跟C的区别,C中赋值后会与0进行比较,如果大于0,就认为是true:而Jav ...

  4. origin添加两个Y轴

    1. 选中X和两个Y 2. 点击Double Y 3. 关掉gap to Symbol,否则Line+Symbol这种显示方式可能显示不出线条 ​

  5. vue 用别名取代路径引用

    在项目开发过程中有可能很多包是没有放在npm上的,许多包需要下载到本地引用,这样一来我们只能通过require的方式来引用文件,但是路径的名字就会很长 例如 import Select from '. ...

  6. List集合,对象根据某个相同的属性,合并另外属性

    描述及实现: 1.List中有 Modular字段值有重复一样的2.Modular字段一样的话去重并且OrderAmount相加 HashMap<String,DataReport> te ...

  7. ABP 数据访问 - IRepository 仓储

    ABP系列,这个系列来的比较晚,很多大佬其实已经分析过,为什么现在我又来一轮呢? 1.想自己来完整的学习一轮ABP 2.公司目前正在使用ABP,准备迁移Core 基于以上的目的,开始这个系列 ABP ...

  8. PHP代码审计04之strpos函数使用不当

    前言 根据红日安全写的文章,学习PHP代码审计的第四节内容,题目均来自PHP SECURITY CALENDAR 2017,讲完题目会用一个实例来加深巩固,这是之前写的,有兴趣可以去看看: PHP代码 ...

  9. python 作业 用python实现 mysql查询结果导出带列名

    1 import pandas as pd 2 import numpy as np 3 import matplotlib as mpl 4 import matplotlib.pyplot as ...

  10. 面经手册 · 第16篇《码农会锁,ReentrantLock之公平锁讲解和实现》

    作者:小傅哥 博客:https://bugstack.cn 专题:面经手册 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 Java学多少才能找到工作? 最近经常有小伙伴问我,以为我的经验来看 ...