Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Douglas Wyatt

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Java filecopy using NIO

   1  
   2      public static void fileCopy( File in, File out )
   3              throws IOException
   4      {
   5          FileChannel inChannel = new FileInputStream( in ).getChannel();
   6          FileChannel outChannel = new FileOutputStream( out ).getChannel();
   7          try
   8          {
   9  //          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows
  10  
  11              // magic number for Windows, 64Mb - 32Kb)
  12              int maxCount = (64 * 1024 * 1024) - (32 * 1024);
  13              long size = inChannel.size();
  14              long position = 0;
  15              while ( position < size )
  16              {
  17                 position += inChannel.transferTo( position, maxCount, outChannel );
  18              }
  19          }
  20          finally
  21          {
  22              if ( inChannel != null )
  23              {
  24                 inChannel.close();
  25              }
  26              if ( outChannel != null )
  27              {
  28                  outChannel.close();
  29              }
  30          }
  31      }

Example file download servlet

// description of your code here

   1  
   2    /**
   3       *  Sends a file to the ServletResponse output stream.  Typically
   4       *  you want the browser to receive a different name than the
   5       *  name the file has been saved in your local database, since
   6       *  your local names need to be unique.
   7       *
   8       *  @param req The request
   9       *  @param resp The response
  10       *  @param filename The name of the file you want to download.
  11       *  @param original_filename The name the browser should receive.
  12       */
  13      private void doDownload( HttpServletRequest req, HttpServletResponse resp,
  14                               String filename, String original_filename )
  15          throws IOException
  16      {
  17          File                f        = new File(filename);
  18          int                 length   = 0;
  19          ServletOutputStream op       = resp.getOutputStream();
  20          ServletContext      context  = getServletConfig().getServletContext();
  21          String              mimetype = context.getMimeType( filename );
  22  
  23          //
  24          //  Set the response and go!
  25          //
  26          //
  27          resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
  28          resp.setContentLength( (int)f.length() );
  29          resp.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
  30  
  31          //
  32          //  Stream to the requester.
  33          //
  34          byte[] bbuf = new byte[BUFSIZE];
  35          DataInputStream in = new DataInputStream(new FileInputStream(f));
  36  
  37          while ((in != null) && ((length = in.read(bbuf)) != -1))
  38          {
  39              op.write(bbuf,0,length);
  40          }
  41  
  42          in.close();
  43          op.flush();
  44          op.close();
  45      }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS