1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.pfe.slide;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author jowo
*/
public class SlideServlet extends HttpServlet {
// Constants ----------------------------------------------------------------------------------
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
private static final String IMG_DIRECTORY = "C:\\images";
// Properties ---------------------------------------------------------------------------------
public static File lastSlide=null ;
// Actions ------------------------------------------------------------------------------------
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
File[] list = new File(IMG_DIRECTORY).listFiles();
File inter;
for( int i=(list.length-1); i>0; i--)
{
for( int j=0; j<i; j++)
{
if (list[j].lastModified()> list[j+1].lastModified())
{
inter= list[j];
list[j]= list[j+1];
list[j+1]= inter;
}
}
}
lastSlide= list[list.length-1];
// Check if file actually exists in filesystem.
if (!lastSlide.exists()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. !!!!!!!!
return;
}
//--------------------------------------------------------------------------------
// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("image/png");
response.setHeader("Content-Length", String.valueOf(lastSlide.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + lastSlide.getName() + "\"");
// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(lastSlide), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > -1) {
output.write(buffer, 0, length);
}
output.flush();
} finally {
// Gently close streams.
close(output);
close(input);
}
}
// Helpers (can be refactored to public utility class) ----------------------------------------
private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
// Do your thing with the exception. Print it, log it or mail it.
e.printStackTrace();
}
}
}
} |