图片存放目录F:\work\hanz\WebRoot\upload  
图片表:photolist
id   imagurl
1    upload/XXXX.JPG1
2    upload/XXXX.JPG2
我要做的是后台上传图片!我前台的图片显示是通过读取photolist表中imagurl中图片路径来显示图片的!
希望做过网站后台图片上传的前辈给与指点!

解决方案 »

  1.   

    用struts的 文件上传,
    FileOutputStream fos = FileOutputStream("F:\work\hanz\WebRoot\upload"+myFile.getFileName());
    fos.write...
    fos.flush..
    然后在这 往数据库里 插一条 数据 
    id                          imagurl 
    myFile.getFileName()        "upload"+myFile.getFileName()
    fos.close();不知道行不行。你用的是 struts 吗?
      

  2.   

    纯JSP +SERVLET  这么干 也可以吧?不清楚。等答案。
      

  3.   

    1、JSP页面中
      <form action="upload.do" method="post" enctype="multipart/form-data">
    文件名:  <input type="text" name="filename"/><br>
    选择文件:<input type="file" name="file" value="select"/><br>
    <input type="submit" value="submit"/>
      </form />2、建ActionForm   名为 uploadActionForm
                private String filename;
    private FormFile file;
          getter and setter
    3、建Action
     UpLoadActionForm ulf = (UpLoadActionForm)form;
    FormFile file = ulf.getFile();
    FileOutputStream fos = new FileOutputStream("F:\work\hanz\WebRoot\upload"+ulf.getFilename());
    fos.write(file.getFileData());
    fos.flush();
                      //在这里往数据库里插  相应数据 
    fos.close();
      

  4.   

    这个是 struts1.x  不知道 LZ是不是 用的 1.Xstruts2   没弄过 不过 估计和这个差不多。
      

  5.   

    你数据库保存的是具体文件呢,还是文件名?1. 具体文件的话,文件流直接上传啊。
    2. 文件名,那就是简单的isnert操作。读取则相反啊。你是哪步不会?
      

  6.   

    添加数据时 就写个insert方法啊
    file文本框得到的也是路径
    在insert方法里面时 
    if(this.isTokenValid(request)){  //struts令牌
         this.saveImg(request, imgFile, fileForm); //保存图片
         fileService.insertFile(fileForm.getFile());//插入数据
         this.resetToken(request);//struts令牌
    }//保存图片
    private void saveImg(HttpServletRequest request,FormFile imgFile,FileForm fileForm){
    if (imgFile != null && imgFile.getFileSize() > 0) {
    String fileName = imgFile.getFileName();
    String sqlPath = "img/" + fileName;
    //图片所在路径
    String savePath = request.getSession().getServletContext().getRealPath("/")+ "img\\" + fileName;
    System.out.println(fileName);
    System.out.println(sqlPath);
    System.out.println(savePath);
    HttpSession session=request.getSession();
    session.setAttribute("savePath", savePath);
    session.setMaxInactiveInterval(60*60);
    //String savePath1=(String)session.getAttribute("savePath");
    // 数据库
    fileForm.getFile().setFileEmpPhoto(sqlPath);
    // 文件
    try {
    InputStream input = imgFile.getInputStream();
    FileOutputStream output = new FileOutputStream(savePath);
    byte[] b = new byte[1024];
    while (input.read(b) != -1) {
    output.write(b);
    b = new byte[1024];
    }
    output.close();
    input.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  7.   

    给你个例子
    public ActionForward insertFile(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    FileForm fileForm=(FileForm)form;
    System.out.println(fileForm.getFile().getPositionName().getPositionNameId());
    FormFile imgFile=fileForm.getImgFile();

    try {
    if(this.isTokenValid(request)){
    this.saveImg(request, imgFile, fileForm);
    fileService.insertFile(fileForm.getFile());

    this.resetToken(request);
    }
    response.setContentType("text/html;charset=GBK"); 
    response.setCharacterEncoding("GBK"); 
    PrintWriter writer = response.getWriter(); 
    writer.write(" <script>alert('档案信息登记成功!');window.location.href='/HR_four/file.do?action=findAllFile'; </script>"); 
    writer.close(); 
    return null; 
    // return this.findAllFile(mapping, form, request, response);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return mapping.findForward("error");
    }
    }-----------------------//保存图片
        private void saveImg(HttpServletRequest request,FormFile imgFile,FileForm fileForm){
            if (imgFile != null && imgFile.getFileSize() > 0) {
                String fileName = imgFile.getFileName();
                String sqlPath = "img/" + fileName;
                //图片所在路径
                String savePath = request.getSession().getServletContext().getRealPath("/")+ "img\\" + fileName;
                System.out.println(fileName);
                System.out.println(sqlPath);
                System.out.println(savePath);
                HttpSession session=request.getSession();
                session.setAttribute("savePath", savePath);
                session.setMaxInactiveInterval(60*60);
                //String savePath1=(String)session.getAttribute("savePath");
                // 数据库
                fileForm.getFile().setFileEmpPhoto(sqlPath);
                // 文件
                try {
                    InputStream input = imgFile.getInputStream();
                    FileOutputStream output = new FileOutputStream(savePath);
                    byte[] b = new byte[1024];
                    while (input.read(b) != -1) {
                        output.write(b);
                        b = new byte[1024];
                    }
                    output.close();
                    input.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
      

  8.   

    我要做的就是路径要插入在photolist表中,图片上传到WebRoot/uploadfile中
      

  9.   

    上传的图片文件存放在uploadfile文件夹中,数据库中只有图片的路径
    比如photolist
    id   imageaurl
    1    uploadfile/xxxx.jpg