1:jsp页面
<form action="UploadServlet" method="post" enctype="multipart/form-data" > 学号:<input name="userid"/><br> 姓名:<input name="username"/><br> 上传照片:<input type="file" name="spicture"/><br> <input type="submit" value="注册"> </form>
2:servlet页面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ?? ??? ?request.setCharacterEncoding("UTF-8"); ?? ??? ?response.setCharacterEncoding("utf-8"); ?? ??? ?response.setContentType("text/html; charset=UTF-8"); ?? ??? ?System.out.println("其他字段"); ?? ??? ?int id=0; ?? ??? ?String name = null; ?? ??? ?String filepath; ?? ??? ?//上传 ?? ??? ?try { ?? ??? ??? ?Boolean isMultipart=ServletFileUpload.isMultipartContent(request); ?? ??? ??? ?if(isMultipart)//判断jsp页面是否有mutipart属性 ?? ??? ??? ?{ ?? ??? ??? ??? ?FileItemFactory factory=new DiskFileItemFactory(); ?? ??? ??? ??? ?ServletFileUpload upload=new ServletFileUpload(factory); ?? ??? ??? ??? ?//通过parseRequest解析form中的所有请求字段并保存到items集合中 ?? ??? ??? ??? ?List<FileItem> items=upload.parseRequest(request); ?? ??? ??? ??? ?//遍历Items中的数据 ?? ??? ??? ??? ?Iterator<FileItem> iter=items.iterator(); ?? ??? ??? ??? ?while(iter.hasNext()) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?FileItem item=iter.next(); ?? ??? ??? ??? ??? ?String itemName=item.getFieldName();//getFieldName获取普通字段 ?? ??? ??? ??? ??? ?//判断前台字段是普通form表单字段(userid,username)还是文件字段 ?? ??? ??? ??? ??? ?if(item.isFormField()) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?if(itemName.equals("userid")) ?? ??? ??? ??? ??? ??? ?{//根据name属性值item是(userid,username,spicture) ?? ??? ??? ??? ??? ??? ??? ? id=Integer.parseInt(item.getString("utf-8")); ?? ??? ??? ??? ??? ??? ??? ? System.out.println(id); ?? ??? ??? ??? ??? ??? ?}else if(itemName.equals("username")) ?? ??? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ??? ? name=item.getString("utf-8"); ?? ??? ??? ??? ??? ??? ?}else ?? ??? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ??? ?System.out.println("其他字段"); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ??? ?//spicture文件上传 ?? ??? ??? ??? ??? ??? ?//文件名 ?? ??? ??? ??? ??? ??? ?String filename=item.getName();//getName()获取文件名 ?? ??? ??? ??? ??? ??? ?//获取文件内容并上传 ?? ??? ??? ??? ??? ??? ?//定义文件路径 ?? ??? ??? ??? ??? ??? ?System.out.println(filename); ?? ??? ??? ??? ??? ??? ?String path="E:\\jsp实验\\UpAndDown\\WebContent\\upload"; ?? ??? ??? ??? ??? ??? ?filepath=path+"\\"+filename; ?? ??? ??? ??? ??? ??? ?System.out.println(filepath); ?? ??? ??? ??? ??? ??? ?File file=new File(path,filename); ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ??? ??? ?item.write(file); ?? ??? ??? ??? ??? ??? ??? ?Student student=new Student(id,name,filepath); ?? ??? ??? ??? ??? ??? ??? ?UserDao dao=new UserdaoImpl(); ?? ??? ??? ??? ??? ??? ??? ?Boolean start=dao.Insert(student); ?? ??? ??? ??? ??? ??? ??? ?if(start=true) ?? ??? ??? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ??? ??? ?System.out.println("成功"); ?? ??? ??? ??? ??? ??? ??? ?}else ?? ??? ??? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ??? ??? ?System.out.println("失败"); ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?} catch (Exception e) { ?? ??? ??? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?}catch(FileUploadException e) ?? ??? ?{ ?? ??? ??? ?e.printStackTrace(); ?? ??? ?}
}
3:实体类
public class Student { ?? ?private int userid; ?? ?private String username; ?? ?private String filename; ?? ?public int getUserid() { ?? ??? ?return userid; ?? ?} ?? ?public void setUserid(int userid) { ?? ??? ?this.userid = userid; ?? ?} ?? ?public String getUsername() { ?? ??? ?return username; ?? ?} ?? ?public void setUsername(String username) { ?? ??? ?this.username = username; ?? ?} ?? ?public String getFilename() { ?? ??? ?return filename; ?? ?} ?? ?public void setFilename(String filename) { ?? ??? ?this.filename = filename; ?? ?} ?? ?public Student(int userid, String username, String filename) { ?? ??? ?super(); ?? ??? ?this.userid = userid; ?? ??? ?this.username = username; ?? ??? ?this.filename = filename; ?? ?} ?? ? }
4:数据库操作
//DBconnection.DBcon()这是自己封装的一个链接数据库方法返回对象为Connection
我就不写了需要的评论区见
public class UserdaoImpl implements UserDao ?{ ?? ?public Boolean Insert(Student student) { ?? ??? ?Boolean start=false; ?? ??? ?PreparedStatement pstmt=null; ?? ??? ? ?? ??? ?try { ?? ??? ??? ?String sql="insert into student values(?,?,?)"; ?? ??? ??? ?pstmt=DBconnection.DBcon().prepareStatement(sql); ?? ??? ??? ?pstmt.setInt(1, student.getUserid()); ?? ??? ??? ?System.out.println(student.getUserid()); ?? ??? ??? ?pstmt.setString(2, student.getUsername()); ?? ??? ??? ?pstmt.setString(3, student.getFilename()); ?? ??? ??? ?int count=pstmt.executeUpdate(); ?? ??? ??? ?if(count>0) ?? ??? ??? ?{ ?? ??? ??? ??? ?start=true; ?? ??? ??? ??? ?return start; ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?return start; ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?}finally ?? ??? ?{ ?? ??? ??? ?if(pstmt!=null) ?? ??? ??? ?{ ?? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ?pstmt.close(); ?? ??? ??? ??? ??? ?DBconnection.DBcon().close(); ?? ??? ??? ??? ?} catch (SQLException e) { ?? ??? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return start; ?? ?} ?? ??? ?
} ?
|