servlet上传图片 服务器路径(转)
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上传图片 服务器路径(转)的更多相关文章
- servlet上传图片 服务器路径
1.在servlet中上传图片,上传的文件夹是imge在webroot下,主要代码如下 private void saveImage(HttpServletRequest request, HttpS ...
- JAVA获取服务器路径的方法
1.在JSF环境中获取到ServletContext: 1 2 ServletContext sc = (ServletContext)FacesContext. getCurrent ...
- JavaWeb学习之什么是Servlet、如何使用servlet、为什么这样使用、servlet的虚拟路径、关于缺省Servlet(2)
1.什么是Servlet? * 服务器端Java程序,servlet需要交给服务器来运行. * 与javax.servlet.Servlet接口有关的java程序 2.如何使用servlet?[必须] ...
- servlet缺省路径
servlet缺省路径 servlet的缺省路径(<url-pattern>/</url-pattern>)是在tomcat服务器内置的一个路径.该路径对应的是一个Defaul ...
- UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器
1.从相册里面选取图片 /// <summary> /// 1.1 从相册里面选取图片 /// </summary> /// <param name="send ...
- java中servlet的各种路径
1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...
- Tortoise 下修改服务器路径(Relocate与Switch)
今天遇到SVN的路径变化,要在客户端修改服务器的下载路径,当初想直接删除重新checkout,后来想着还要重建项目比较麻烦,就找找修改服务器路径的方法.网上基本说的都是右键-->Relocate ...
- file上传图片获取路径地址
file上传图片获取路径地址 类似:点击button,选择图片,在对应的输入框input里面显示图片路径地址 类似这样 原理:通过opacity来隐藏原生的input file 然后用.file-bt ...
- JAVA记录-JSP页面获取服务器路径方式
1.basePath方式 <% String path = request.getContextPath(); String basePath = request.getScheme()+&qu ...
随机推荐
- php-fpm日志告警"seem busy"
php-fpm日志中出现"WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.m ...
- 纯CSS3技术 加载中
你能相信吗?这些都是由一个DIV元素实现的动画,纯CSS3技术 html <div class="loader">加载中...</div> css: 图( ...
- iwebshop 改版页面
- WDLINUX (Centos5.8) 安装 bcmath
环境 centos5.8 php5.2.17 因为wdos 集成的php5.2.17为精简版,并未包含php52-bcmath扩展. 所以先下载完整php5.2.17源码包 wget -c http: ...
- window.opener方法的使用 js跨域
原文:window.opener方法的使用 js跨域 最近公司网站登陆加入了第三方登陆.可以用QQ直接登陆到我们网站,在login页面A中点QQ登陆时,调用了一个window.open文件打开一个lo ...
- 电子科大POJ "a^b"
a ^ b Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) C-sources: ...
- php float 转int
round(x,prec) 参数 描述 x 可选.规定要舍入的数字. prec 可选.规定小数点后的位数. <?php echo(round(0.60)); echo(round(0.50)); ...
- hdu 5606 tree(并查集)
Problem Description There is a tree(the tree is a connected graph which contains n points and n−1 ed ...
- android xml布局文件属性说明
android xml布局文件属性说明 [摘]android xml布局文件属性说明 LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件androi ...
- Fiddler 跟踪 手机页面数据包
随着 HTML5 的急速增长,现在越来越多的人,开始涉及到移动终端的 Web 开发领域,但手机端始终没有 PC 端这么多的调试工具.即使 PC 端浏览器模拟 user-agent 进行开发,也可能会发 ...