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 ...
随机推荐
- java Timer 使用小结
Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务.这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少. 任务的调用通过起的子线程进 ...
- Weex 开发入门
去年也听说过 React Native 技术,现在好像很多大公司都在这套技术中踩坑,在开发自己的UI.工作中涉及不到,一直没有学习相关的知识. 并且听说阿里的 vue native,一直很期待.前段时 ...
- struts2笔记12-声明式异常
1.配置异常处理 <action name="save" class="com.test.actions.ProductAction" method=&q ...
- Axure 快捷方式
基本快捷键: 打开:Ctrl + O新建:Ctrl + N保存:Ctrl + S退出:Alt + F4打印:Ctrl + P查找:Ctrl + F替换:Ctrl + H复制:Ctrl + C剪切:Ct ...
- Struts2 + Spring + hibernate 框架搭成实例
1.准备Jar包: struts2.hibernate.spring所需jar包 struts-core-2.x.x.jar ----struts核心包 xwork-core-2.x.x.jar ...
- Pods 更新后提示Bundle资源找不到
http://www.oschina.net/question/101347_2159145
- Android快捷支付SDK Demo resultStatus={4001};memo={參数错误};result={}问题
在支付宝中粘贴RSA公钥并提交,然后问题就完美攻克了...
- sharepoint 2013 reporting services 远程server返回错误: (500) 内部server错误。
在sharepoint 2013部署reporting services过程中,点击管理中心,server上的服务.系统配置.提示了一个错误: 远程server返回错误: (500) 内部server ...
- Java 可中断线程
PART.1 无法中断的线程 一个无法中断的线程的例子. public class UninterruptableThread { @SuppressWarnings("deprecatio ...
- oracle&&Sqlserver获取表名列名主键及数据类型
SQlserver获得列名,列类型,列类型长度,scale,prec等数据类型(syscolumns,systypes,sysobjects均为视图) select a.name as colname ...