<form action="register.do" enctype="multipart/form-data" method="post"> <p>真实姓名:</p> <input type="text" name="question1" size="30"><br> <p>出生年月:</p> <input type="text" name="question2" size="30"><br> <p>地址:</p> <input type="text" name="question3" size="30"><br> <p>民族:</p> <p><select name="question4"> <option value="汉族">汉族</option> <option value="回族">回族</option> <option value="布依族">布依族</option> <option value="壮族">壮族</option> </select> </p> <p>兴趣爱好:</p> <input type="checkbox" name="question5" value="看书">看书 <input type="checkbox" name="question5" value="运动">运动 <input type="checkbox" name="question5" value="乒乓球">乒乓球 <input type="checkbox" name="question5" value="篮球">篮球 <input type="checkbox" name="question5" value="跑步">跑步 <input type="checkbox" name="question5" value="听歌">听歌 <input type="checkbox" name="question5" value="泡吧">泡吧 <input type="checkbox" name="question5" value="约会">约会<br> <p>头像:</p> <input type="file" name="filename" size="30"><br> <br><br> <!-- 上传多个文件的代码 --> <!-- <p>文件:</p> <input type="file" multiple name="filename1" size="30"><br> <br><br> --> <input type="submit" value="确认"> <input type="reset" value="取消">
</form>
@WebServlet(name="Register",urlPatterns={"/register.do"}) //设置文件存储的路径 @MultipartConfig(location="D:\\",fileSizeThreshold=1024) public class Register extends HttpServlet { private static final long serialVersionUID = 1L; private static final Object[] OK = null; //定义提示信息 String show = ""; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //新建一个result是为了记录图片的URL String result = null; request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //创建输出流准备写入数据 PrintWriter out = response.getWriter(); //显示页面servlet中的一个基础 out.println("<html><body><div style=\"width:300px;margin:0 auto;\">"); //获取register界面的注册信息然后显示给用户 String quest1 = request.getParameter("question1"); String quest2 = request.getParameter("question2"); String quest3 = request.getParameter("question3"); String quest4 = request.getParameter("question4"); String quest5[] = request.getParameterValues("question5"); //显示注册信息 out.println("<h2 style=\"color:red;\">你的注册信息为:</h2>"); //——————头像代码开始——————// String fname=""; String path = this.getServletContext().getRealPath("/");//获得根目录 Part filename = request.getPart("filename");//获得文件名 // //判断是否选择了头像 // if(filename.toString()==null){ // show = "请选择头像!谢谢"; // request.setAttribute("show", show); // RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); // rd.forward(request, response); // } if(filename.getSize()>1024*1024){ filename.delete(); out.println("文件太大,不能上传!"); }else{ path=path+"image\\";//存放图片的路径 File f=new File(path); if(!f.exists()){ f.mkdirs(); } String h=filename.getHeader("content-disposition"); fname=h.substring(h.lastIndexOf("filename")+10,h.length()-1);//得到文件名 filename.write(path+fname); result= path+fname;//获得当前上传文件的路径 out.println("头像上传成功!"+"<br><br>"); out.println("头像路径为:<br>"+"<div style=\" word-wrap: break-word; word-break: normal; \">"+result+"</div><br>"); } //接下来是显示头像的代码 out.println("头像为:"+"<br><br>"); //显示图片 out.println("<img src='image\\"+fname+"' width=300px height=200px/>"); //——————头像代码结束——————// out.println("<br><br>"); //——————显示信息开始——————// if(!quest1.equals("")){ out.println("真实姓名为:" + quest1); out.println("<br>"); } else{ show = "请填写姓名!谢谢"; request.setAttribute("show", show); RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); rd.forward(request, response); } if(!quest2.equals("")){ out.println("出生年月为:" + quest2); out.println("<br>"); } else{ show = "请填写出生年月!谢谢"; request.setAttribute("show", show); RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); rd.forward(request, response); } if(!quest3.equals("")){ out.println("地址为:" + quest3); out.println("<br>"); } else{ show = "请填写地址!谢谢"; request.setAttribute("show", show); RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); rd.forward(request, response); } if(!quest4.equals("")){ out.println("民族为:"+quest4); out.println("<br>"); } if(quest5!=null){ out.println("兴趣爱好为:"); for(int i=0;i<8;i++) out.println(quest5[i]); out.println("<br>"); } else{ show = "请填写完成所有信息!谢谢"; request.setAttribute("show", show); RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); rd.forward(request, response); } //——————显示信息结束——————// //显示页面结束 out.println("</div></body></html>"); } }
javaweb关于文件上传2
最新推荐文章于 2024-07-03 14:15:35 发布