JSP页面使用文件上传图片并且回传显示的注意事项
来源:互联网 发布:淘宝司法拍卖房产网址 编辑:程序博客网 时间:2024/05/20 11:20
***要正常实现文件上传以下步骤应该全部考虑到:
*1.html中 <inputtype="file">表示文件上传控件
*2.form的enctype="multipart/form-data"
*3.在Servlet类前加上 @MultipartConfig
*4.request.getPart()获得上传文件对象 //request.getParameter();
以下代码全是从我的代码了复制过来的,没有花太多的时间整理,先看一下效果图吧
<span style="color:#0000ff;"><!-- 这里是文件上传表单的开始 --> <form action="/imas/UploadImage" method="post" </span><span style="color:#ff6666;">enctype="multipart/form-data"</span><span style="color:#0000ff;">><input type="hidden" name="updateId" value="<%=operaterEntity.getId() %>" /><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr> <td width="18%" background="<%=basePath%>tab/images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">上传照片</span></div></td> <td bgcolor="#FFFFFF"> <input type="file" </span><span style="color:#ff6666;">name="pic"</span><span style="color:#0000ff;"> /> <input type="submit" value="上传"> </td> </tr></table></form></span>
//下面代码是文件上传部分,功能主要是获取到上传图片的路劲和图片名称,然后上传图片,并把路径保存到数据库中
@WebServlet("/UploadImage")@MultipartConfigpublic class UploadImage extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("---这里是上传个人图片的servlet----");<span style="color:#ff6666;">Part part = request.getPart("pic");</span>String file = part.getHeader("Content-Disposition");//得到文件的名字String fileName = file.substring(file.lastIndexOf("=")+2, file.length()-1);System.out.println(fileName);//获取部署的根目录String basePath = getServletContext().getRealPath("/");System.out.println(basePath);//将文件上传到部署的根目录下part.write(basePath+fileName);//以下代码是对应我的项目的,可以修改为对应自己项目的代码int id = Integer.parseInt(request.getParameter("updateId"));OperaterImpl operaterImpl = new OperaterImpl();OperaterEntity operaterEntity = operaterImpl.findById(id);operaterEntity.setpicPath(basePath+fileName);operaterImpl.update(operaterEntity);response.getWriter().write("<script>window.location.href='tab/updateOperater.jsp';</script>");}}
@WebServlet("/ShowImageSer")public class ShowImageSer extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//从前台获取图片的路劲(部署项目的根路径)此路劲必须包含到图片,//如D:\My\SOFTWORE\apache-tomcat-7.0.52\wtpwebapps\imas\111.gif<span style="color:#3366ff;">String picPath = request.getParameter("picpath");</span><span style="color:#ff6666;">//以该路劲创建一个新文件File file = new File(picPath);</span>response.setCharacterEncoding("gb2312");response.setContentType("doc");response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("gb2312"),"iso8859-1"));System.out.println(new String(file.getName().getBytes("gb2312"),"gb2312"));OutputStream output = null;FileInputStream fis = null;try{output = response.getOutputStream();fis = new FileInputStream(file);byte[] b = new byte[1024];int i = 0;while((i = fis.read(b))!=-1){output.write(b, 0, i);}output.write(b, 0, b.length);output.flush();response.flushBuffer();}catch(Exception e){System.out.println("Error!");e.printStackTrace();}finally{if(fis != null){fis.close();fis = null;}if(output != null){output.close();output = null;}}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub}}//这是显示图片的区域,使用img标签,在src里面访问到servlet类(ShowImageSer),并且传递图片路劲参数过去
<img style="width: 71px; height: 99px;" alt="请上传个人图片" src="<%=basePath%>ShowImageSer?<span style="color:#3366ff;">picpath</span>=<%=operaterEntity.getpicPath() %>">
0 0
- JSP页面使用文件上传图片并且回传显示的注意事项
- SpringMVC+Ajax用FormData对象上传页面的图片(文件),并且立马在页面上显示出来
- jsp页面上传图片之后页面,当前页面的显示上传的图片可以自行更新
- struts中用kindeditor实现的图片上传并且显示在页面上
- JSP 上传图片+显示图片的问题
- 图片上传的JSP文件
- PHP上传图片并且显示
- 在JSP表单提交中怎样在Servlet中处理文件上传并且获取JSP页面传过来的一些参数
- jsp页面 上传图片预览 的处理
- jsp/html页面图片上传并展示上传的图片
- jsp页面上传图片文件到服务器页面无刷新的技巧
- java中图片上传并在jsp页面显示
- JSP页面,实现上传图片即时显示效果
- 上传图片然后显示在JSP页面上
- struts2+ajax图片上传以及jsp页面显示
- jsp中图片的上传和显示
- JSP中上传图片显示的问题
- JSP中图片的上传与显示
- Windows下安装MySql-5.7.14
- Intelij导入maven项目并部署到tomcat
- 解决DrawerLayout的不能全屏滑动的问题
- ajax
- MIPS汇编语言学习
- JSP页面使用文件上传图片并且回传显示的注意事项
- 把mp3文件编码为base64, 然后再把base64为转换为mp3
- 图片三级缓存
- android 记录崩溃日志
- 微信小程序开发<二>文件结构解析
- 一个合格的程序员应有的修炼书
- 单元测试mockito初体验
- 蜂蜜
- php常用魔术方法