Hello there,
I'm using hibernate in my project, this project allow users to upload file to the server and these files are being saved on the database. This is working just fine, but when I try to download a file it takes too long to load from the database and start the download. What I do to send the file to the user is use a servlet to load this file from the database and write to the output stream.
This is my servlet that sends the file:
Code:
public class Arqs extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
Arquivo arq = ArquivoJPA.recuperar(Long.parseLong(id));
System.out.println(arq.getId());
System.out.println(arq.getNome());
System.out.println(arq.getEnctype());
response.setContentType(arq.getEnctype());
response.setHeader("Content-Disposition", "attachment; filename=\"" + arq.getNome() + "\"");
response.getOutputStream().write(arq.getArquivo());
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
And this is my Entity File:
Code:
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Arquivo implements Serializable {
@TableGenerator(name="arquivo_geradorid", table="arquivo_geradorid", allocationSize=1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator="arquivo_geradorid")
private Long id;
private String nome;
private String enctype;
private String extensao;
@Column(length=33554432)
private byte[] arquivo;
... getters and setters
}
Is there anyway to make this loading faster? I mean I know that the problem is that is taking too long to load, because for a file of 2 megabytes it takes around 20 seconds to load and then it gives me the option to save or open.
Thanks in advance,
Bruno Krebs.