jsp上传文件用 servlet
<form name="form1" method="post" action="photoservlet"  enctype="multipart/form-data">
  <table width="309" height="229"  align="center">
    <tr>
      <td width="106">姓名:</td>
      <td width="240"><input type="text" name="name"></td>
    </tr>
    <tr>
      <td height="47">专业:</td>
      <td><input type="text" name="type"></td>
    </tr>
    <tr>
      <td height="48">照片:</td>
      <td><input type="file" name="photo" ></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="添加">
      <input type="reset" name="Submit2" value="重置"></td>
    </tr>
  </table>
</form>在Servlet中
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;public class photoServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=GBK";
    private String uploadPath = "image\\doctor\\";
    private String tempPath = "image\\doctor\\"; // 用于存放临时文件的目录
    //Initialize global variables
    public void init() throws ServletException {
    }    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("gbk");
        response.setCharacterEncoding("gbk");    }    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        doGet(request, response);
        try {
if(!new File(uploadPath).isDirectory()){
new File(uploadPath).mkdirs();
}
     DiskFileUpload fu = new DiskFileUpload();
     // 设置最大文件尺寸,这里是4MB
     fu.setSizeMax(4194304);
     // 设置缓冲区大小,这里是4kb
     fu.setSizeThreshold(4096);
     // 设置临时目录:
     fu.setRepositoryPath(tempPath);
     // 得到所有的文件:
     List fileItems = fu.parseRequest(request);
     Iterator i = fileItems.iterator();
     // 依次处理每一个文件:
     while (i.hasNext()) {
         FileItem fi = (FileItem) i.next();
         // 获得文件名,这个文件名包括路径:
         String fileName = fi.getName();
         if (fileName != null) {
         fi.write(new File(uploadPath
                     + "\\" + fileName.substring(fileName.lastIndexOf("\\") + 1)));
         String name = request.getParameter("name");
         System.out.print(name);
         }
     }
    response.sendRedirect("adddoctor.jsp");
 } catch (Exception e) {
     // 可以跳转出错页面
 }
    }    //Clean up resources
    public void destroy() {
    }
}同时我要把jsp中的信息写如数据库 应该怎么样实现哪位高手是否可以详细讲解  

解决方案 »

  1.   

    在你的JSP处理器servlet中写入数据库即可呀,你可以调用你的数据库操作类来完成这样的操作
      

  2.   

    把姓名 专业写出db?
    有啥不可的
    jdbc 存呗
    request获得这些字段  ......
      

  3.   

    就是获取不了  字段  不晓得是什么原因   request获取的全是null值  
      

  4.   

    说错了,用struts2,非常方便。这里竟然不允许修改自己的帖子,真郁闷。
      

  5.   

    struts2  不会 可以说的详细点不?
      

  6.   

    commons-fileupload,如果使用它解析了request,就只能再从它的里面获得parameter了
    以前的request里的数据已经都转移到它的解析类里了,你再查查api呗。