还有怎么显示图片,数据库里的表里只有图片文件名 元素,

解决方案 »

  1.   

    上传按扭的图片: <input type="file" id="upload" />显示图片: 从数据库中取出文件路径及名称,用action或者servlet得到文件,转换成byte[]格式,然后
    response.setContentType("image/jpeg");
    OutputStream out = response.getOutputStream();
    out.write(byte[]);
    以下是将inputStream转换成byte的方法,楼主可以参考
    //将stream转化为byte
    private byte[] inputStreamToByte(InputStream inStream)
    {
    if(inStream != null)
    {
    try {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = inStream.read()) != -1) {
            byteStream.write(ch);
        }
        byte imgData []=byteStream.toByteArray();
        return imgData;
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
    return null;
    }
      

  2.   

    控件可以用commons-fileupload-1.2.jar等很多
      

  3.   

    用jspSmartUpload组件上传~~
    存数据库的时候存图片的名称和路径就成了,当然,象大小等信息也可以存
      

  4.   

    Struts fu.jsp 
    <%@ page language="java" pageEncoding="gb2312"%> 
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>  
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> 
      
    <html>  
    <head> 
    <title>JSP for FuForm form </title> 
    </head> 
    <body> 
    <html:form action="/fu.do" method="POST" enctype="multipart/form-data"> 
    <html:file property="file"/> 
    <html:submit/> <html:cancel/> 
    </html:form> 
    </body> 
    </html> imgs.jsp 
    <%@ page language="java" pageEncoding="gb2312"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html:html locale="true"> 
       <head> 
         <title>imgs.jsp </title> 
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    -->    </head> 
       
       <body> 
       <img src="${namePath}" /> 
       </body> 
    </html:html> error.jsp 
    <%@ page language="java" pageEncoding="gb2312"%> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html:html> 
       <head> 
         <title>error.jsp </title> 
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    -->    </head> 
       
       <body style="font-size:12px"> 
         <font color="red">${str} </font> 
       </body> 
    </html:html> FuAction.java 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) { 
    FuForm fuForm = (FuForm) form;// TODO Auto-generated method stub 
    Session ses = HibernateSessionFactory.getSession(); 
    Transaction tra = ses.beginTransaction(); 
    Imgs imgs = new Imgs(); 
    ImgsDAO idao = new ImgsDAO(); 
    String fu = ".jpg ¦.gif"; 
    Pattern pa = Pattern.compile(fu); 
    FormFile ff = fuForm.getFile(); 
    String fileName = ff.getFileName(); 
    String pathName = "/Fu/imgs/" + fileName; 
    System.out.println("fileName"+fileName); 
    int fileSize = ff.getFileSize(); 
    List list = idao.findByNamePath(pathName); 
    if(fileSize > 5000){ 
    String str = "您上传的图片过大!"; 
    request.setAttribute("str", str); 
    return mapping.findForward("error"); 
    }else if(list.size() > 0){ 
    String str = "您上传的图片已存在!"; 
    request.setAttribute("str", str); 
    return mapping.findForward("error"); 
    }else{ 
    imgs.setNamePath(pathName); 
    idao.save(imgs); 
    tra.commit(); 

    String fileType = ff.getContentType(); 
    System.out.println("fileType"+fileType); 
    Matcher mat = pa.matcher(fileName); 
    if(mat.find()){ 
    System.out.println("是否找到:"+"find"); 
    try{ 
    byte b[] = ff.getFileData(); 
    String namePath = "D:\\java\\Fu\\WebRoot\\imgs\\"+fileName; 
    FileOutputStream fos = new FileOutputStream(namePath); 
    fos.write(b); 
    fos.close(); 
    System.out.println("文件名:"+fileName); 
    }catch (Exception e) { 
    // TODO: handle exception 
    e.printStackTrace(); 

    }else{ 
    System.out.println("是否找到:"+"not find"); 

    System.out.println(list.size()); 
    imgs = (Imgs) list.get(0); 
    System.out.println(imgs.getNamePath()); 
    request.setAttribute("namePath", imgs.getNamePath()); 
    return mapping.findForward("imgs"); 
    } FuForm.java 
    private FormFile file; 
    public FormFile getFile() { 
    return file; 
    } public void setFile(FormFile file) { 
    this.file = file; 
    } 数据库就两个字段 
    id identity(1,1) 
    NamePath
      

  5.   

    我也是用的commons-fileupload-1.2.jar在<html>标签中只传如上传成功后的文件名.