JSP页面使用文件上传图片并且回传显示的注意事项

来源:互联网 发布:淘宝司法拍卖房产网址 编辑:程序博客网 时间:2024/05/20 11:20

***要正常实现文件上传以下步骤应该全部考虑到:

*1.html <inputtype="file">表示文件上传控件

*2.formenctype="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>");}}


//以下是回显的servlet类,主要获取到图片的根目录,修改它就可以
@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
原创粉丝点击