【问题标题】:How to get the directory path of a text file "test.txt" and split it dynamically?如何获取文本文件“test.txt”的目录路径并动态拆分?
【发布时间】:2014-03-04 23:43:16
【问题描述】:

我创建了一个应用程序,它使用 Tomcat&.0 服务器上传文件并将其存储到 C:\temp\ 目录中。

index.html

<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <form  action="upload.jsp"  method="post"  enctype="multipart/form-data">
     Select File <input type="file" name="file1">
    <p>
     Select Filename <input type="text" size="20" name="filename">
    <p>
    <input type=submit value="Upload">
 </form>
</body>
</html>

上传.jsp

<%@page import="org.apache.commons.fileupload.*,java.util.*,java.io.*"%>

<%
   // JSP to handle  uploading


   // Create a new file upload handler 
   DiskFileUpload upload = new DiskFileUpload();

   // parse request
   List items = upload.parseRequest(request);

   // get uploaded file 
   FileItem  file = (FileItem) items.get(0);
   String source = file.getName();

   // get taget filename
   FileItem  name = (FileItem) items.get(1);
   String  target = name.getString();


   File outfile = new File("C:/temp/" +  target);
   file.write(outfile);

   out.println("Upload Is Successful!");

%>
<jsp:forward page="split.jsp"/>

现在,我想使用 split.jsp 文件拆分存储在 C:\temp\ 目录中的上传文件。

我无法在运行时动态传递源路径名,当我手动提供路径名时它可以正常工作。 例如:字符串输入文件 = "C:/temp/textfie.txt";

这里是完整的代码: 拆分.jsp

<%@page import="java.io.*"%>
<%@page import="java.io.InputStreamReader,java.util.Scanner,java.io.FileInputStream"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
try{  
  // Reading file and getting no. of files to be generated  
  String inputfile = "C:/temp/textfie.txt"; //  Source File Name.  
  double nol = 10.0; //  No. of lines to be split and saved in each output file.  
  File file = new File(inputfile);  
  Scanner scanner = new Scanner(file);  
  int count = 0;  
  while (scanner.hasNextLine())   
  {  
   scanner.nextLine();  
   count++;  
  }  
  out.println("Lines in the file: " + count);     // Displays no. of lines in the input        file.  

  double temp = (count/nol);  
  int temp1=(int)temp;  
  int nof=0;  
  if(temp1==temp)  
  {  
   nof=temp1;  
  }  
  else  
  {  
   nof=temp1+1;  
  }  
  out.println("No. of files to be generated :"+nof); // Displays no. of files to be generated.  

  //------------------------------------------------------------------------------------    ---------------------  

  // Actual splitting of file into smaller files  

  FileInputStream fstream = new FileInputStream(inputfile);
  DataInputStream in = new DataInputStream(fstream);  

  BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
  String strLine;  

  for (int j=1;j<=nof;j++)  
  {  
   FileWriter fstream1 = new FileWriter("C:/text/File"+j+".txt");     // Destination File Location 
   //FileWriter fstream2 = new FileWriter("D:/File"+j+".java"); 
   BufferedWriter out1 = new BufferedWriter(fstream1);   
   //BufferedWriter out1 = new BufferedWriter(fstream2);
   for (int i=1;i<=nol;i++)  
   {  
        strLine = br.readLine();   
        if (strLine!= null)  
        {  
        out1.write(strLine);   
        //out1.write(strLine);
        if(i!=nol)  
        {  
        out1.newLine();
        //out1.newLine();  
        }  
        }  
   }
   /*source= "C:/New Folder/File1.java";
       destination="D:/New Folder";
 // public void copyFile(File source,File destination);*/
 // String source="C:/New Folder";
 // String dest="D:/New Folder";
  /*try {
    //FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
    e.printStackTrace();
}*/

   out1.close();  
   //out1.close();
  }  

  in.close();  
 }catch (Exception e)  
 {  
  System.err.println("Error: " + e.getMessage());  
 }  

%>

【问题讨论】:

  • 您在尝试动态传递文件时使用了什么?
  • 我什么都没用。我只想知道是否有任何接口可以通过它检索上传的文件路径并将其传递给 String filepath="C:\temp\text.txt" 。

标签: java jsp


【解决方案1】:

您需要将上传的文件路径保留在upload.jsp的会话中,并从split.jsp中使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多