good morning,
I want to recover a file that I have saved in the database, I have it in bytes, but I do not know how I can recover it, I want that pressing a button that file is downloaded to a local folder.
This is the code I use to upload it to the BD:
HTML:
<div class="form-group">
<label class="col-sm-2 control-label">Archivo:</label>
<div class="col-sm-10">
<div id="dadjunto1" class="input-group">
<input type="text" id="txtFile1" name="txtFile1" class="form-control required" style="margin-left:10px" readonly="readonly" />
<div class="input-group-btn">
<label class="btn btn-primary">
<span aria-hidden="true">SELECCIONAR ARCHIVO</span>
<input type="file" id="adjunto1" name="adjunto1" style="display:none" onchange="$('input[name=\'txtFile1\']').val($('input[name=\'adjunto1\']').val().replace(/C:\fakepath\/i, '')).trigger('change'); return false;"/>
</label>
</div>
</div>
</div>
</div>
Servlet:
protected void doPost(HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException {
byte[] archivobyte= archivo.obtenerBytes(request );
//Guardo archivobyte en la base de datos
}
Class file:
public static byte[] obtenerBytes(HttpServletRequest request ) {
byte[] fileArray = new byte[1024];
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
List formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
for (Object item : formItems){
FileItem uploaded = (FileItem) item;
if (!uploaded.isFormField()) {
InputStream input = uploaded.getInputStream();
input.read(fileArray);
}
}
}
return fileArray;
}