提交文件,servlet是要从request里的数据流中读取的,和路径没关系。建议用一些组件吧,可以用comoonfileupload之类的。

解决方案 »

  1.   

    我就实想读取一个本地的txt文件,路径和名字不能写死,通过表单传送。
      

  2.   

    <%
    response.setDateHeader("Expires",0);
    %>
    <html>
    <%@ page contentType="text/html;charset=gb2312" import="java.util.ArrayList"%>
    <%@ page import="com.jspsmart.upload.SmartUploadException" %>
    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
    <jsp:useBean id="pfile" scope="page" class="hnccbean.PubFilebean"/>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>文件传送</title>
    </head>
    <%!
    public String DelBlank(String Str_In)
    {
      String Str_Out = "";
      String Tmp = "";
      for(int h=0;h<Str_In.length();h++)
      {
        Tmp = Str_In.substring(h,h+1);
        if(!Tmp.equals(" "))
          Str_Out += Tmp;
      }
      System.out.println(Str_Out);
      return Str_Out;
    }
    %>
    <body>
    <%
    try{
    String g_agentID = (String)session.getAttribute("g_agentID"); 
    if ( g_agentID == null )
    {
    out.println("<script LANGUAGE=\"javaScript\" FOR=\"window\" EVENT=\"onload()\">");
    out.println(" top.location.href = \"login.jsp\"");
    out.println("</script>");
    return;
    } mySmartUpload.initialize(pageContext);
    mySmartUpload.upload();
    String stroper = mySmartUpload.getRequest().getParameter("opertmp")+",";
    ArrayList fileName = new ArrayList();
    int filelen = mySmartUpload.getFiles().getCount();
    String tmpid = pfile.GetMaxID();
    String myFileName = "";
    String myFilePath = "";
      for (int i=0;i<filelen;i++)
      {
        com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
        if (!myFile.isMissing())
        {
          myFileName = myFile.getFileName();
          myFileName = "("+tmpid+")"+myFileName;
         fileName.add(myFileName);
          myFileName = DelBlank(myFileName);
          myFilePath = "file/store/"+myFileName;
          myFile.saveAs("/manage/file/store/"+myFileName,mySmartUpload.SAVE_VIRTUAL);
          try{
           pfile.InsertFileInfo(g_agentID,stroper,myFileName,myFilePath);
          }catch(Exception ex){
            continue;
            //System.out.println("file file_add.jsp error:"+ex);
          }
    }
    }
    %>
    <script language="javascript">
      alert("文件上传成功");
      location.href="file_addfrm.jsp";
    </script>
    <%
    }catch(Exception e){
    %>
    <script language="javascript">
      alert("文件上传出错");
    </script>
    <%
    }finally{
      if(pfile!=null)
        pfile.dbclose();
    }
    %>
    </body>
    </html>
      

  3.   


    servlet中用
    String path = request.getParameter("filepath");path就是你获得的文件标签里的文件路径。