我用 jspsmart组件 做上传文件,我把图片上传到服务器,但当tomcat部署 Remove 掉项目时 服务器的图片就全没了,这样项目拿到别的机子运行就没有图片,所以我把图片的名字+后缀 保存到数据库 但不知道当项目运行时如何将数据库的图片自动上传到服务器上或者不要保存到数据库,如何把图片直接保存在MyEclipse的项目中的upload的文件夹 ( upload文件夹是保存图片的 )这样就是双份保存 如何做到这是上传的代码:uploadimage.jsp 页面 <%
     SmartUpload mySmartUpload=new SmartUpload();
     long file_size_max=4000000;
     String fileName2="",ext="",testvar="";
     String url="upload/";
     mySmartUpload.initialize(pageContext);
    try{
     mySmartUpload.setAllowedFilesList("jpg,gif");
     mySmartUpload.upload();
     }
     catch(Exception e)
     {
     %>
     <script type="text/javascript">
      alert("只允许上传.jpg和.gif类型图片文件");
     </script>
     <%
      }
     String name=null;
        try
      {
   com.jspsmart.upload.File myFile=mySmartUpload.getFiles().getFile(0);
     //若文件不存在则继续  
     if(myFile.isMissing())
     {
        %>
      <script type="text/javascript">       alert("请选择要上传的文件");       window.location='upload.htm';      </script>  
    <%
    }
    else{     //String myFileName=myFile.getFileName(); //取得上载的文件的文件名     ext=myFile.getFileExt(); //取得后缀名     int file_size=myFile.getSize();     String saveurl="";      if(file_size<file_size_max)
      {
       //更改文件名,取得当前上传时间的毫秒数值       Calendar calendar=Calendar.getInstance();       String filename=String.valueOf(calendar.getTimeInMillis());       name=filename;       saveurl=application.getRealPath("/")+url;       saveurl+=filename+"."+ext;       // 将文件另存到以WEB应用程序的根目录为文件根目录的目录下     
       myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);
  }
    }
      }
      catch(Exception e)
    {     out.print(e.toString());    }    //ImageDao是一个图片的dao层
    ImageDao imgDao=new ImageDao();    System.out.println(name+"."+ext);   //用AddImageName的方法添加一张图片名称到数据库中,不是图片的全路径,是图片名而已
   int result=imgDao.AddImageName(name+"."+ext);     if(result>0)
     {
      response.sendRedirect("success.jsp");
     }
     else
     {
      out.print("<script language=javascript>alert('添加到数据库失败');</script>");
     }
     %>upload.jsp 页面 
<html>
  <head>
    <title>掉</title>
   <script type="text/javascript">
    function checkfile()
    {
   
   if(document.form1.file.value==null)
   {
   alert('去死');
   return false;
   }    
   return true;
     }
   </script>
</head>
  <body>
<table border="0" align="center" cellpadding="0" cellspacing="0"><tr><td height="45" align="center" valign="middle">
<form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form1" onsubmit="return checkfile()"><input type="file" name="file"><input type="submit" name="Submit" value="上传"></form></td></tr></table>

解决方案 »

  1.   

    当tomcat部署 Remove 掉项目时 服务器的图片就全没了如果是这样的话,你存储图片的路径就有问题了。你可以不存储到项目下的文件夹中
      

  2.   

    一般使用目录保持upload目录的方式,实现上都不不是直接放到tomcat下的,最好是有一个配置,通过这个配置来确定保存到那个目录里,而且一般这样的形式,图片都是保持在专门的文件服务器上,提供静态服务,在自己应用里有一个配置表明这个提供静态服务的专用的文件服务器的url是怎样的,从而是部署和发布很方便。
      

  3.   

    我是说项目部署好了  tomcat的webapps中就有这个项目的upload的文件夹 ,这时就会有图片上传,而在myeclipse中的这个项目的upload文件夹中没有图片的,图片在部署好的项目中而没有在myeclipse的项目中,而项目移植到别的地方就没有图片了,所以才保存在数据库,问题是:大家给个建议 如何一加载图片读进部署好的tomcat/webapps的项目中 或者 不上传到服务器也不要保存在数据库 保存在 myeclipse中的这个项目中  myeclipse中的upload这个文件夹虽然和tomcat中的upload这个文件夹一样 不过图片是会在服务器那边的upload文件夹的
      

  4.   

    把你的saveurl设成服务器硬盘上的其它路径,这样就算被重新部署了,上传的文件还会存在。
      

  5.   


    这个让我有点明白了  那如何获得当前 导入myeclipse中的项目的路径啊  假设我现在在随便的机子的myeclipse中导入而我不知道
    他的workspace在那个路径如何获得  整个项目的全路进
      

  6.   

    为什么要myeclipse目录呢,生产环境是不会有myeclipse的。你可以随便设置一个硬盘目录就可以了。
      

  7.   

    晕  是动态获得啊  大哥  是叫我写死啊   假设我现在在随便的机子的myeclipse中导入而我不知道 
    他的workspace在那个路径如何获得  整个项目的全路进 
      

  8.   

    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      

  9.   

    我是意思是,把这个路径放在myeclipse的项目中本身就不合理,项目路径中只是源码而已,最终是要部署到应用服务器的路径中的。
      

  10.   

    大哥  你获取错了 现在图片都不知道跑到哪里  动态获取 E:\MyEclipse 7.0M1\workspace2\Uppass
    你刚才教我 项目-右键-属性,能看到项目路径但String path = request.getContextPath(); 
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";不对啊 获取是这样的  http://localhost:8080/Uppass/upload/  图片都不知道上哪了
      

  11.   

    其实就很简单的回答我  怎么样  动态获得当前打开的  myeclipse的工作区的路径 就好了
      

  12.   

    String dir = null; 
    dir="D:\\os\\newproject\\GrpRWeb\\WebContent\\UploadImages"; 
    dir+"/"+ + imageFileName;
    或者dir=ServletActionContext.getServletContext().getRealPath("/UploadImages") 
      

  13.   

    ServletActionContext  这个单词完全没有  别玩我了
      

  14.   

    String dir = null; 
    dir = request.getSession(true).getServletContext().getRealPath("/UploadImages");
    你就不能变通下,上边给你的是struts2的
    dir="D:\\os\\newproject\\GrpRWeb\\WebContent\\UploadImages";这个也不好使?
      

  15.   

    建议把图片上传路径写在应用路径外。比如说:你之前用的是应用相对路径,你可以把上传路径改为服务器绝对路径。例如: /home/upload/photo这样的话会出现另外一个问题,也就是上传图片展现的问题,我是做路径映射来解决。或许别人有更好的办法
      

  16.   

    给你一个好的解决方法你看看:
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.upload.web.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.upload.FormFile;/**
     * MyEclipse Struts Creation date: 08-16-2009
     * 
     * XDoclet definition:
     * 
     * @struts.form name="uploadForm"
     */
    public class UploadForm extends ActionForm {
    /*
     * Generated fields
     */ /** theFile property */
    private FormFile theFile; /*
     * Generated Methods
     */ /**
     * Method validate
     * 
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) { System.out.println(theFile.getContentType());
    ActionErrors errors = new ActionErrors();
    if (theFile.getFileName() == null
    || theFile.getFileName().trim().length() < 1) {
    errors.add("fileNull", new ActionMessage("upload.null.error"));
    }
    // 判断文件的类型
    // image/GIF
    else if (!theFile.getContentType().startsWith("image")) {
    errors.add("badFile", new ActionMessage("upload.bad.error"));
    } return errors;
    } /**
     * Method reset
     * 
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    // TODO Auto-generated method stub
    } /**
     * Returns the theFile.
     * 
     * @return FormFile
     */
    public FormFile getTheFile() {
    return theFile;
    } /**
     * Set the theFile.
     * 
     * @param theFile
     *            The theFile to set
     */
    public void setTheFile(FormFile theFile) {
    this.theFile = theFile;
    }
    }
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.upload.web.action;import java.io.File;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DownloadAction;public class DownAction extends DownloadAction { @Override
    protected StreamInfo getStreamInfo(ActionMapping mapping,
    ActionForm actionForm, HttpServletRequest request,
    HttpServletResponse response) throws Exception { // 1.设置响应的内容类型为下载
    response.setContentType("application/x-msdownload;charset=GBK");
    HttpSession session = request.getSession();
    String file = (String) session.getAttribute("FILE");
    String fileName = file.substring(file.lastIndexOf("/"));
    fileName = fileName.substring(1);
    response.setHeader("Content-Disposition", "attachment;fileName="
    + fileName); File f = new File(file); String contentType = (String) session.getAttribute("CONTENT"); return new FileStreamInfo(contentType, f);
    }
    }
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.upload.web.action;import java.io.FileOutputStream;
    import java.util.UUID;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;import com.upload.web.form.UploadForm;public class UploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    UploadForm uploadForm = (UploadForm) form;
    // 1.获得保存上传文件绝对路径
    String path = servlet.getServletContext().getRealPath("/userFile"); // 2.获得用户上传的文件名
    FormFile theFile = uploadForm.getTheFile();
    String fileName = theFile.getFileName(); String houzhui = fileName.substring(fileName.lastIndexOf(".")); path += "/" + UUID.randomUUID() + houzhui; HttpSession session = request.getSession();
    session.setAttribute("FILE", path);
    session.setAttribute("CONTENT", theFile.getContentType()); // 3.文件输出流,向指定的文件输出信息(写出)
    FileOutputStream fout = new FileOutputStream(path); // 4.获得用户上传文件的数据
    byte fileData[] = theFile.getFileData(); // 5.上传
    fout.write(fileData); return mapping.findForward("toDown");
    }
    }
    <%@ page contentType="text/html; charset=GBK"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%><html>
    <head>
    <title>文件上传</title>
    </head> <body bgcolor="#CCCCCC">
    <h2>
    Struts文件上传
    </h2>
    <hr />
    <html:errors/>
    <!-- 注意:
    1.表单必须包含encType属性,且值为:multipart/form-data

     -->
    <html:form action="upload" enctype="multipart/form-data">
    请选择您要上传的文件:
    <html:file property="theFile" />
    <html:submit value="上传" /> </html:form>
    </body>
    </html>
      

  17.   

    <%@ page contentType="text/html; charset=GBK"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
    <html>
    <head>
    <title>Struts文件下载</title>
    </head> <body>
    <h2>
    Struts文件下载
    </h2>
    <hr /> <a href="down.do">下载到客户端</a>
    <a href="down.do">在线打开</a> </body>
    </html>
      

  18.   

    假如说你的图片是上传到你的工程名下的一个叫images的文件夹里,你就在webRoot下建一个同样的文件名字,把你要一运行就要加载的图片放在里面。
    不过不建意议你这么做,你只需要把需要首次加载的文件放在images(工作空间)里就行了,上传来的文件单独放到一个文件夹里比如叫upload(tomcat)。如果你还想要你的这些上传后的文件,那你就得多费一点事,把这个文件夹拷到webRoot(工作空间)下,下一次tomcat会自动给你布暑上去的。
      

  19.   

    不过Struts俺还没学  会慢慢研究的