file控件点击浏览按钮,获取他选择文件的路径,然后根据这个路径读取文件里面的内容,将他显示到文本框里,怎么做啊求教实现的功能是 点击浏览的时候你选择一个文件,这时候这个file控件里就有了选择的文件的路径,想通过这个路径,把选择的文件的内容读取到 文本框中,这个要怎么实现,求解啊~~

解决方案 »

  1.   

    用ajax 吧。上传后,把文件内容传到页面。
    参照 http://hi.baidu.com/chjie219/blog/item/73429a824d4ed698f603a6ca.html
      

  2.   


      public class ImageServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { this.doPost(request, response);
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    MultipartRequest mr = null;
    int maxPostSize = 1*100*1024*1024;
    String dir = getServletConfig().getServletContext().getRealPath("/uploadImage");
    mr = new MultipartRequest(request,dir,maxPostSize,"utf-8");
    Enumeration files = mr.getFileNames();
    String fileName ="";
    String path = "";
    while(files.hasMoreElements()){

    fileName = files.nextElement().toString();
    path = mr.getFilesystemName(fileName);
    File f = mr.getFile(fileName);

    if(f==null){
    throw new ServletException("file is null");
    } Date dt = new Date(System  
                        .currentTimeMillis());  
                SimpleDateFormat fmt = new SimpleDateFormat(  
                "yyyyMMddHHmmssSSS");  
                String time = fmt.format(dt); 
    String newName = time+f.getName().substring(path.lastIndexOf('.'));
    File newFile = new File(dir+"/"+newName);
    f.renameTo(newFile);
    System.out.println("newName:"+newName);
    System.out.println("filename:"+newFile.getName());
    System.out.println("path:"+newFile.getPath());
    }
    PrintWriter out = response.getWriter();
    out.print("上传成功!");
    }}
      

  3.   

    需要导入MultipartRequest 类需要的架包