1.在servlet中上传图片,上传的文件夹是imge在webroot下,主要代码如下

    private void saveImage(HttpServletRequest request, HttpServletResponse response) throws IOException  {
//保存的图片的名称
fileName =System.currentTimeMillis() + ".jpg";
//获得imge文件夹在tomcat中的决定路径,basePath的值是C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.20\webapps\flexTest\imge
String basePath = request.getSession().getServletContext().getRealPath("/imge/");
filePath = basePath;
System.out.println("保存图片的地址为:"+filePath);
realFilePath = filePath+"\\"+fileName;
// 获得一个图片文件流,我这里是从flex中传过来的
BufferedImage bufferedImage = ImageIO.read(request.getInputStream());
if (bufferedImage != null) {
//保存图片到指定的目录和文件中
ImageIO.write(bufferedImage, "jpeg", new File(filePath , fileName));
}
}

2.当要把上面上传图片通过servlet展示到游览器上时,取的路径如下:

private void printImage(HttpServletRequest request, HttpServletResponse response) throws IOException  {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
//获得服务器的地址,不能直接获取本机tomcat的绝对路径,不然游览器读取不了指定的图片文件
// basePath的值是http://localhost:8080/flexTest
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
String showFile = basePath+"/imge/"+this.fileName;
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(" <img src="+showFile+">");
out.print("</img>");
System.out.println("有显示图片的地址是"+showFile);
//showFile的值是 http://localhost:8080/flexTest//imge/1299470395060.jpg
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();

servlet上传图片 服务器路径(转)的更多相关文章

  1. servlet上传图片 服务器路径

    1.在servlet中上传图片,上传的文件夹是imge在webroot下,主要代码如下 private void saveImage(HttpServletRequest request, HttpS ...

  2. JAVA获取服务器路径的方法

    1.在JSF环境中获取到ServletContext: 1 2 ServletContext sc = (ServletContext)FacesContext.         getCurrent ...

  3. JavaWeb学习之什么是Servlet、如何使用servlet、为什么这样使用、servlet的虚拟路径、关于缺省Servlet(2)

    1.什么是Servlet? * 服务器端Java程序,servlet需要交给服务器来运行. * 与javax.servlet.Servlet接口有关的java程序 2.如何使用servlet?[必须] ...

  4. servlet缺省路径

    servlet缺省路径 servlet的缺省路径(<url-pattern>/</url-pattern>)是在tomcat服务器内置的一个路径.该路径对应的是一个Defaul ...

  5. UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器

    1.从相册里面选取图片 /// <summary> /// 1.1 从相册里面选取图片 /// </summary> /// <param name="send ...

  6. java中servlet的各种路径

    1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...

  7. Tortoise 下修改服务器路径(Relocate与Switch)

    今天遇到SVN的路径变化,要在客户端修改服务器的下载路径,当初想直接删除重新checkout,后来想着还要重建项目比较麻烦,就找找修改服务器路径的方法.网上基本说的都是右键-->Relocate ...

  8. file上传图片获取路径地址

    file上传图片获取路径地址 类似:点击button,选择图片,在对应的输入框input里面显示图片路径地址 类似这样 原理:通过opacity来隐藏原生的input file 然后用.file-bt ...

  9. JAVA记录-JSP页面获取服务器路径方式

    1.basePath方式 <% String path = request.getContextPath(); String basePath = request.getScheme()+&qu ...

随机推荐

  1. Python zxing 库解析(条形码二维码识别)

    各种扫码软件 最近要做个二维码识别的项目,查到二维码识别有好多开源的不开源的软件 http://www.oschina.net/project/tag/238/ Zbar 首先试了一下Zbar,pyt ...

  2. join函数——Gevent源码分析

    在使用gevent框架的时候,我们经常会使用join函数,如下: def test1(id): print(id) gevent.sleep(0) print(id, 'is done!') t = ...

  3. Inno Setup 系统托盘图标插件 TrayIconCtrl V1.5

    原文 http://restools.hanzify.org/article.asp?id=93 V1.5 修正在某些 Windows 平台上(例如 Windows XP SP3)不能正常运行的问题. ...

  4. TLV----Demo讲解

    接触过网络协议的人对TLV一定或多或少的知道.作为一种自定义应用层标准. TLV使用十分广泛.他对数据封包有着很好的定义,简单实用. TLV即Type-Length-Value.即我们每个封装成TLV ...

  5. C++关联容器综合应用:TextQuery小程序

    本文介绍C++关联容器综合应用:TextQuery小程序(源自C++ Primer). 关于关联容器的概念及介绍,请参考园子里这篇博文:http://www.cnblogs.com/cy568sear ...

  6. LDA 资料整理

    LDA 中文名叫 隐含狄利克雷分布 有一个讲的数学八卦的pdf,如下: http://pan.baidu.com/s/1bnX6Pgb Latent Dirichlet Allocation(LDA) ...

  7. git备忘录

    1.git: patch does not apply git apply --ignore-space-change --ignore-whitespace mychanges.patch 2.Ge ...

  8. STL set接口中使用结构体类型

    需要在结构体中重载'<'运算符,下面是我写的一个例子: #include<iostream> #include<set> using namespace std; str ...

  9. c/c++性能优化--- cache优化的一点杂谈

    之前写了一篇关于c/c++优化的一点建议,被各种拍砖和吐槽,有赞成的有反对的,还有中立的,网友对那篇博客的的评论和吐槽,我一个都没有删掉,包括一些具有攻击性的言论.笔者有幸阅读过IBM某个项目的框架代 ...

  10. Java替代C语言的可能性

        前不久CSDN刊登了一篇<C语言已经死了>的文章,引起了一些争论.事实上那篇文章是从Ed Burnette的博客上转载来的,原文题目是“Die, C, die!”,直译过来应该是& ...