You need to keep the uploaded file path in session from upload.jsp and use it from split.jsp
How to get the directory path of a text file "test.txt" and split it dynamically?
Question
I have created an application which uploads a file and stores it into C:\temp\ directory using Tomcat&.0 server.
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"/>
Now, I want to split the uploaded file stored in the C:\temp\ directory using split.jsp file.
I cannot pass the source path name dynamically during runtime it works fine when i give the path name manually. ex: String inputfile = "C:/temp/textfie.txt";
here is the complete code: 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());
}
%>
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow