how to get or call a uploaded file from a previous page to the next page in java and jsp codes

This is the codes for uploading a file.

upload.jsp

this code uploads a file into the defined directory and the uploaded file is given into the codes of the splitting page given below.

<%@page import="java.io.*, java.sql.*"%>
<%! static String saveFile; %>
<%


saveFile="newfile";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename="") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\") + 1,saveFile.indexOf("""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename="");
pos = file.indexOf("n", pos) + 1;
pos = file.indexOf("n", pos) + 1;
pos = file.indexOf("n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile="F:/Project/Upload/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

%>

<%
 out.println(saveFile);

response.sendRedirect("split.html");
     }
%>

the called file should be called into this page and then directed to the split.jsp page.then the html page should perform the form action.

split.html

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Classic Enquiry Form a Flat Responsive Widget Template :: w3layouts </title>
    <!-- Meta tags -->
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="Classic Enquiry Form Responsive Widget, Audio and Video players, Login Form Web Template, Flat Pricing Tables, Flat Drop-Downs, Sign-Up Web Templates, Flat Web Templates, Login Sign-up Responsive Web Template, Smartphone Compatible Web Template, Free Web Designs for Nokia, Samsung, LG, Sony Ericsson, Motorola Web Design"
    />
    <script type="application/x-javascript">
        addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); }
    </script>
    <!-- Meta tags -->
    <!--stylesheets-->
    <link href="css/style3.css" rel='stylesheet' type='text/css' media="all">
    <!--//style sheet end here-->
    <!-- font-awesome icons-agile -->
    <link rel="stylesheet" href="css/font-awesome.min.css" />

    <!-- //font-awesome icons-agile -->
    <link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet">
</head>

<body>
    <FORM ENCTYPE="multipart/form-data" ACTION="split.jsp" METHOD="post">
    <h1 class="header-w3ls">
        Split </h1>             <div class="btnn">
                        <input type="submit" value="Split Files">
                    </div>
                </div>
        </form>

Splitting the file

  • this is the code for splitting a file into chunks.only a user defined
    file can be called using this code.The file that has been uploaded and called in the html page should be called hee after the html page’s form action.

Split.jsp

>     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
>         pageEncoding="ISO-8859-1"%>
>     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@page import="java.io.File"
> %>
>     <%@page import="java.io.FileInputStream" %>
>     <%@page import="java.io.FileOutputStream" %>
>     <%@page import="java.io.InputStream" %>
>     <%@page import="java.io.OutputStream" %>
>     <%@page import="java.util.Arrays" %>
>     <%@ page import="java.sql.*" %>
>     
>     <%
>     try {
>                 File file = new File("F:/Project/Upload/Dolby.mp4");            
>                 if (file.exists()) {
>                   String videoFileName = file.getName().substring(0, file.getName().lastIndexOf(".")); // Name of the videoFile without
> extension
>                 File splitFile = new File("F:/Project/Splitted Files/"+ videoFileName);//Destination folder to save.
>                 if (!splitFile.exists()) {
>                     splitFile.mkdirs();
>                     System.out.println("Directory Created -> "+ splitFile.getAbsolutePath());
>                 }
>     
>                 int i = 01;// Files count starts from 1
>                 InputStream inputStream = new FileInputStream(file);
>                 String videoFile = splitFile.getAbsolutePath() +"/"+ String.format("%02d", i) +"_"+ file.getName();// Location to save the
> files which are Split from the original file.
>                 OutputStream outputStream = new FileOutputStream(videoFile);
>                 System.out.println("File Created Location: "+ videoFile);
>                 int totalPartsToSplit = 5;// Total files to split.
>                 int splitSize = inputStream.available() / totalPartsToSplit;
>                 int streamSize = 0;
>                 int read = 0;
>                 while ((read = inputStream.read()) != -1) {
>     
>                     if (splitSize == streamSize) {
>                         if (i != totalPartsToSplit) {
>                             i++;
>                             String fileCount = String.format("%02d", i); // output will be 1 is 01, 2 is 02
>                             videoFile = splitFile.getAbsolutePath() +"/"+ fileCount +"_"+ file.getName();
>                             outputStream = new FileOutputStream(videoFile);
>                             System.out.println("File Created Location: "+ videoFile);
>                             streamSize = 0;
>                         }
>                     }
>                     outputStream.write(read);
>                     streamSize++;
>                 }
>     
>                 inputStream.close();
>                 outputStream.close();
>                 response.sendRedirect("home.html");
>                
>               ;
>                
>             } 
>                 
>                 else {
>                 System.err.println(file.getAbsolutePath() +" File Not Found.");
>             }
>                 
>                 
>         } 
>     catch (Exception e) {
>             e.printStackTrace();
>     }
>     
>     %>

I want to get the file in the previous upload page to be called into this split page.Please help me with this.