请求帮助  我把图片上传到服务器端,怎么在获得这个图片啊
我获得图片在tomcat下的路径  为什么获得不到图片??

解决方案 »

  1.   

    Struts2图片上传html页面代码:<tr>
                            <td align="right">业务图片:</td>
         <td><input type="file" name="file" id="imgurl" style="border:1px solid #7F9DB9;"/><span class="checkts">&nbsp;&nbsp;&nbsp;&nbsp;*</span><span class="sm">&nbsp;&nbsp;&nbsp;&nbsp;说明:游戏图片</span><br/></td>    
       </tr>
    js代码:var imgurlValue = $('#imgurl').val();  // 图片上传
    if ((imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "jpg")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "jpeg")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "png")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "bmp")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "gif")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "JPG")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "JPEG")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "PNG")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "BMP")
    &&(imgurlValue.substring(imgurlValue.lastIndexOf('.')+1) != "GIF")) {
    //alert("系统不允许上传此种类型文件!");
    $('#errorMsg').html("系统不允许上传此种类型文件");
    return false;
    }
    action 代码:// ********************************************************
    //首先将文件写入一个输入流里面
    InputStream is = new FileInputStream(file);
    //其次得到你要上传文件到那个目录
    String root = ServletActionContext.getRequest().getRealPath("/upload/images");
    DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    String formatDate = format.format(new Date());
    int random = new Random().nextInt(10000);
    int position = fileFileName.indexOf(".");
    String extension = fileFileName.substring(position) ;
    String newFileName = formatDate + random + extension ;
    //再次创建一个File来保存你的文件
    File destFile = new File(root,newFileName);
    //然后就是一个输出流将文件写入到File中。
    OutputStream os = new FileOutputStream(destFile);
    //以下就是写入文件的方式
    byte buffer[] = new byte[2048]; 
    int length = 0;
    while((length = is.read(buffer))>0){
    os.write(buffer,0,length);
    }
    //最后一定要关闭流 
    is.close();
    os.close();
      

  2.   

    html: <input type=file name="imageFile" />java: 
    private File imageFile;getter/setter....FileInputStream in = new FileInputStream(imageFile);//commons-fileupload-1.2.1.jar
      

  3.   

    D:\apache-tomcat-6.0.32\work\Catalina\localhost\forum_system\upload__7ac4141d_131f5f9ce44__8000_00000000.tmp
    这是操作系统路径  用这个路径得不到这个图片
      

  4.   

    struts2 文件上传的例子很多 建议楼主多搜搜 很简单 ,多看看,摸索摸索 ,用点心。
    把图片的名称保存到数据库然后读取出来加上路径的名称就ok
      

  5.   

    jsp页面  upload.jsp
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'upload.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
      <center>
        <form method="post" action="${pageContext.request.contextPath}/uploadAction.action" enctype="multipart/form-data">
         File:<input type="file" name="pic"></input><br />
         <input type="submit" value="上传图片"></input>
        </form>
        </center>
      </body>
    </html>servlet代码package action;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport 
        implements ServletRequestAware{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private File pic;
    private String picFileName;
    private HttpServletRequest request;

    public String getPicFileName() {
    return picFileName;
    } public void setPicFileName(String picFileName) {
    this.picFileName = picFileName;
    } public File getPic() {
    return pic;
    } public void setPic(File pic) {
    this.pic = pic;
    } @Override
    public String execute() throws Exception {
    ServletContext sc = request.getSession().getServletContext();
    String absoultPath = sc.getRealPath("/file");
    FileInputStream in = new FileInputStream(pic);
    FileOutputStream out = new FileOutputStream(absoultPath+"/"+picFileName);
    byte[] b = new byte[1024];
    int length = 0;
    while(true){
    length = in.read(b);
    if(length<0)break;
    out.write(b,0,length);
    }
    in.close();
    out.close();
    System.out.println(picFileName+"bbbbbbbbbbbbbbbb");
    request.getSession().setAttribute("pi",pic);
    System.out.println("file is "+ pic);
    return "ok";
    } public void setServletRequest(HttpServletRequest request) {
    this.request = request;

    }

    }成功跳转页面}<html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'uploadOK.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <center><h1>
        <img src="${pageContext.request.contextPath}/${sessionScope.pic}"/>
        </h1></center>
      </body>
    </html>已经成功跳转到uploadOK,可以使这个页面仅输出一句话,怎么显示图片,很纠结,谢谢各位了
      

  6.   

    pic不能获取到地址吗?pic.getAbsolutePath()
      

  7.   

    这个路径就是pic啊 picFileName只是图片的名字
      

  8.   

    楼主你的路径不对,那个不是你上传的路径,一般是在tomcat下,可是楼主你注意看好
      forum_system\upload__7ac4141d_131f5f9ce44__8000_00000000.tmp
      楼主你见过这种格式的图片吗?
      

  9.   

    可是我输出来的pic就是这个啊,你们说明白点啊,不明白
      

  10.   

    你可以在system.properties中定义你的路径名
    TempFilePath = C:\\1\\
    RealFilePath = C:\\2\\String filepath =  PropertyUtil.getProperty("TempFilePath");
    String drcPath = filepath+compid+File.separatorChar;       
    File drcpath = new File(drcPath);
    File file =new File(drcPath+ this.getUploadFileName());
       if(!drcpath.exists()){
    drcpath.mkdirs(); }
       if(!file.exists()){
       02Service.copy(this.upload, file);
    //   file.delete();
       }
             
    return SUCCESS;
    public void copy(File src, File dst){

             try {   
                InputStream in = null ;   
                OutputStream out = null ;   
                 try {                   
                    in = new BufferedInputStream( new FileInputStream(src), Ass02Constant.BUFFER_SIZE);   
                    out = new BufferedOutputStream( new FileOutputStream(dst), Ass02Constant.BUFFER_SIZE);   
                     byte [] buffer = new byte [Ass02Constant.BUFFER_SIZE];   
                     while (in.read(buffer) > 0 ) {   
                        out.write(buffer);   
                   }    
                } finally {   
                     if ( null != in) {   
                        in.close();   
                    }    
                     if ( null != out) {   
                        out.close();   
                    }    
               }
              
              
            } catch (Exception e) {
               e.printStackTrace();   
           }
           
       }
      

  11.   

    其实在struts2中上传的文件只是一个临时文件 当上传组件代码执行完毕后这个临时文件就删除了你可以
    通过窗口信息提示看到文件创建后又被删除了 所以你需要将你的临时文件copy到你要上传的目标下面这样就可以找到了 希望能帮助楼主
      

  12.   

    这个tmp是临时文件,上传完成后一般会自动删除的,完成后就没用了,你可以把它后缀名改成图片格式的,这样就能打开了
      

  13.   

    要的就是这个copy啊,怎么不说重点呢
      

  14.   

    文件名formatDate + random + extension ?
      

  15.   

    路径用相对路径存放,我记得是request.getContextXXXPath()的,获取相对路径,然后后面写该工程服务器中的文件夹。