如何获取文本文件“test.txt”的目录路径并动态拆分?

时间:2014-03-04 14:20:14

标签: java jsp

我创建了一个应用程序,它使用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>

upload.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 \目录中的上传文件。

我无法在运行时动态传递源路径名称,当我手动提供路径名称时,它工作正常。 ex:String inputfile =“C:/temp/textfie.txt”;

这是完整的代码: split.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());  
 }  

%>

1 个答案:

答案 0 :(得分:0)

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