JavaEE 上传/预览图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangtao510/article/details/79866833
//前台页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>一个简单的JSP</title>
<script type="text/javascript">
 function priview(){  
	 form1.img.src= window.URL.createObjectURL(form1.file1.files[0]);
 }  
</script>
</head>
<body> 
   <form action="UploadServlet" method="post" enctype="multipart/form-data" name="form1">
   <input type="file" name="file1" onchange="priview()">
   <input type="submit" value="上传">
   <img alt="" src="" width="100px" height="100px" name="img"> 
   </form>
</body>
</html>
UploadServlet 
protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String realPath = this.getServletContext().getRealPath("/");
		Part p=request.getPart("file1");
		if(p.getContentType().contains("image")){
			p.write("d:\\a.jpg");
			response.getWriter().print("上传成功!");
		}
	}

猜你喜欢

转载自blog.csdn.net/wangtao510/article/details/79866833
今日推荐