How to Copy a File in Jsp


Java doesn't offer a neat and pretty one-liner for copying files. However, Java's file input-output (I/O) classes make it fairly easy to write your own file copying functionality. Get started by writing the file copy functionality as scriptlet code directly in your Java ServerPages (JSP) page.

The Basics and Catch Clause

  • Import the Java classes you'll need for reading and writing files using the page directive in your JSP page:

    <%@ page import=\"*\" %>

  • Create a try-catch block in your JSP page to handle IOException:

    try {}
    catch (IOException ex) {}

  • Handle IOException errors inside the catch clause as needed for the JSP page to fail gracefully. Print the exception message in glaring colors into the JSP page by breaking out of the scriptlet code:

    catch (IOException ex) { %>
    <strong style=”color: red; background-color:gold;”>
    <% }

The Try Block

  • Open the source file (the file you want to copy) and the destination file (where the copy will be written to) inside the try-block. SrcFileName and dstFileName are string variables containing the path and file name of each file:

    File srcFile = new File(srcFileName)
    File dstFile = new File(dstFileName);

  • Check that the source file exists, and throw an IOException if it doesn't:

    if (!srcFile.exists()) {
    throw new IOException(\"No source file: \" + srcFileName)

  • Check that the destination file exists and is writable. Throw an IOException if it isn't:

    if (dstFile.exists()) {
    if (!dstFile.canWrite()) {

    throw new IOException(\&quot;Destination read-only: \&quot; + dstFileName)

    else {
    throw new IOException(\"Destination not created: \" + dstFileName)

  • Open source and destination file streams:

    FileInputStream srcStrm = new FileInputStream(srcFile)
    FileOutputStream dstStrm = new FileOutputStream(dstFile);

  • Create a byte array to hold data:

    byte[] buf = new byte[4096];

  • Read from the source stream and write to the destination stream in a while loop that continues until all the data have been read from the source file:

    int len
    while ((len = > 0) {
    dstStrm.write(buf, 0, len)

  • Close the file streams:


Tips & Warnings

  • Take it to the next level by making your file copying functionality a JavaBean or custom tag handler class. Create a custom tag that takes the source and destination files as parameters. This practice will make your file copy method reusable and keep your JSP page free of unnecessary scriptlet code.
  • Handle all exceptions. Unhandled exceptions can result in your website visitors seeing pages of ugly Java error messages.
  • Remember, JSP pages are server-side applications, so the source and destination of your file copy is on the web server.

Related Searches


Promoted By Zergnet



You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!