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


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

*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")
@MultipartConfig	
public 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() %>">




  • 12
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值