Changeset 25053


Ignore:
Timestamp:
2012-02-07T13:54:40+13:00 (12 years ago)
Author:
sjm84
Message:

You can now specify a downloadFile argument in the URL to force the web browser to download a file, even if it is an image. Will need to add some form of security for this

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java

    r24993 r25053  
    438438                        }
    439439
    440                        
    441440                        FileChannel source = null;
    442441                        FileChannel destination = null;
     
    484483            String href = null;
    485484            String rl = null;
    486             String[] nameval = new String[2]; // Reuse it for memory efficiency purposes   
    487485
    488486            for (int i = 0; i < query_arr.length; i++)
     
    506504                {
    507505                    rl = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
     506                }
     507            }
     508
     509            for (String arg : query_arr)
     510            {
     511                if (arg.startsWith("downloadFile"))
     512                {
     513                    int index = arg.indexOf("=");
     514                    if (index > -1 && index < arg.length() - 1)
     515                    {
     516                        String fileLocation = arg.substring(index + 1);
     517                        File fileToGet = new File(GlobalProperties.getGSDL3Home() + File.separator + fileLocation);
     518
     519                        if (fileToGet.exists())
     520                        {
     521                            response.setContentType("application/octet-stream");
     522                            response.addHeader("Content-Disposition","attachment;filename=" + fileToGet.getName());
     523                            FileInputStream fis = new FileInputStream(fileToGet);
     524                            ServletOutputStream sos = response.getOutputStream();
     525
     526                            byte[] buffer = new byte[4096];
     527                            int len;
     528                            while ((len = fis.read(buffer)) != -1)
     529                            {
     530                                sos.write(buffer, 0, len);
     531                            }
     532                            sos.flush();
     533                            fis.close();
     534                            sos.close();
     535                           
     536                            return;
     537                        }
     538                    }
    508539                }
    509540            }
Note: See TracChangeset for help on using the changeset viewer.