|
|
|
|
@ -2,16 +2,20 @@ package com.evolute.siprp.pagina;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.text.MessageFormat;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import com.evolute.utils.arrays.ResultSet2DArray;
|
|
|
|
|
import com.evolute.utils.error.ErrorLogger;
|
|
|
|
|
import com.evolute.utils.strings.UnicodeChecker;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class doGetFicheiro extends siprpServlet
|
|
|
|
|
{
|
|
|
|
|
@ -19,22 +23,43 @@ public class doGetFicheiro extends siprpServlet
|
|
|
|
|
|
|
|
|
|
public doGetFicheiro(HttpServletRequest req, HttpServletResponse res, String query) throws IOException, SQLException, ClassNotFoundException
|
|
|
|
|
{
|
|
|
|
|
Class.forName( bdDriver );
|
|
|
|
|
Connection con = DriverManager.getConnection( bdLocalUrl, bdLocalUsername, bdLocalPassword );
|
|
|
|
|
res.addHeader( "Content-disposition", "attachment;filename=\"" + query + "\"" );
|
|
|
|
|
res.setContentType("application/octet-stream");
|
|
|
|
|
OutputStream os = res.getOutputStream();
|
|
|
|
|
PreparedStatement ps = con.prepareStatement("SELECT file_data FROM fil_file where name='" + query + "'");
|
|
|
|
|
ResultSet rs = ps.executeQuery();
|
|
|
|
|
if(rs != null)
|
|
|
|
|
ServletOutputStream os = res.getOutputStream();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while(rs.next())
|
|
|
|
|
ResultSet2DArray rs = getDatabaseData(query);
|
|
|
|
|
if(rs != null)
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0; i <= rs.columnLength(); i++ )
|
|
|
|
|
{
|
|
|
|
|
res.addHeader( "Content-disposition", "attachment;filename=\"" + UnicodeChecker.parseFromUnicode( (String) rs.get(i, 2) ) + "\"" );
|
|
|
|
|
byte[] b = rs.get(i, 0);
|
|
|
|
|
res.setHeader( "Content-Length", String.valueOf( b.length ) );
|
|
|
|
|
res.setContentType((String) rs.get(i, 1));
|
|
|
|
|
os.write(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte[] b = rs.getBytes(1);
|
|
|
|
|
os.write(b);
|
|
|
|
|
}
|
|
|
|
|
os.println( mergeTemplate( msgGenericError, errorTemplate ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch( Exception ex )
|
|
|
|
|
{
|
|
|
|
|
ErrorLogger.logException(ex);
|
|
|
|
|
os.println( mergeTemplate( "Erro ao obter o ficheiro.", errorTemplate ) );
|
|
|
|
|
}
|
|
|
|
|
os.flush();
|
|
|
|
|
os.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResultSet2DArray getDatabaseData(String query) throws Exception
|
|
|
|
|
{
|
|
|
|
|
ResultSet2DArray rs;
|
|
|
|
|
Class.forName( bdDriver );
|
|
|
|
|
Connection con = DriverManager.getConnection( bdLocalUrl, bdLocalUsername, bdLocalPassword );
|
|
|
|
|
PreparedStatement ps = con.prepareStatement( MessageFormat.format("SELECT file_data, mime_type, name FROM fil_file where id=''{0}''", query) );
|
|
|
|
|
ErrorLogger.log( "sql = " + ps.toString( ) );
|
|
|
|
|
rs = new ResultSet2DArray( ps.executeQuery( ) );
|
|
|
|
|
return rs;
|
|
|
|
|
}
|
|
|
|
|
}
|