Java 让浏览器保存下载的文件
我正在使用一个应用程序学习JavaEE文件处理 我正在使用Netbeans 8.0.1、Wildfly 8、JPA和Primefaces 我只有一个具有4个属性的对象 @Id@GeneratedValue(策略=GenerationType.IDENTITY) 私人国际身份证Java 让浏览器保存下载的文件,java,http,browser,download,Java,Http,Browser,Download,我正在使用一个应用程序学习JavaEE文件处理 我正在使用Netbeans 8.0.1、Wildfly 8、JPA和Primefaces 我只有一个具有4个属性的对象 @Id@GeneratedValue(策略=GenerationType.IDENTITY) 私人国际身份证 private String titulo; private String descripcion; @Lob private File archivo; I made a form to upload data
private String titulo;
private String descripcion;
@Lob
private File archivo;
I made a form to upload data including the file as a blob, the form call a method in a backing bean
public void generarArticulo() throws IOException{
File destFile= new File(fichero.getFileName());
FileUtils.copyInputStreamToFile(fichero.getInputstream(), destFile);
articulo a = new articulo();
a.setArchivo(destFile);
a.setTitulo(titulo);
a.setDescripcion(descripcion);
this.controlador.registrarArticulo(a);
}
此方法工作正常,记录被添加到数据库中
然后我还制作了一个datatable,它工作正常,显示了数据库中的每个记录,还测试了每个文件是否被检索,我使用了一个outputtext,它以字节为单位给出了每个文件的权重,并且做得很好
<p:dataTable var="articulos" value="#{listadoArticulos.listado}"
rows="10"
paginator="true"
>
<p:column headerText="Titulo" sortBy="#{articulos.titulo}" >
<h:outputText value="#{articulos.titulo}" />
</p:column>
<p:column headerText="Descripcion" >
<h:outputText value="#{articulos.descripcion}" />
</p:column>
[B]<p:column headerText="Fichero" >
<h:outputText value="#{articulos.archivo.name} y pesa #{articulos.archivo.length()}" />
</p:column>[/B]
<p:column headerText="Descarga">
<p:commandLink action="#{articuloBean.getFichero(articulos.archivo)}" value="Descargar"/>
</p:column>
</p:dataTable>
该方法从数据库中获取存储的文件并将其复制到/home/alex/files文件夹中,我要做的是使该方法正常下载直接在objects file属性中分配的文件
有什么想法吗?我真的解决了这个问题
@WebServlet("/DownloadFileServlet")
public class DownloadFileServlet extends HttpServlet {
@Inject
ArticuloControlador controlador;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String x=request.getParameter("x");
int id = Integer.parseInt(x);
articulo a = controlador.getArticuloporID(id);
File f = a.getArchivo();
FileInputStream inStream = new FileInputStream(f);
String relativePath = getServletContext().getRealPath("");
System.out.println("relativePath = " + relativePath);
// obtengo ServletContext
ServletContext context = getServletContext();
// obtengo MIME del fichero
String mimeType= URLConnection.guessContentTypeFromName(f.getName());
if (mimeType == null) {
// steamos el MIME type si no lo encontramos
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// modificamos el response
response.setContentType(mimeType);
response.setContentLength((int) f.length());
// Descargamos
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", f.getName());
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
}
}
不能对数据库中的Blob使用
FileInputStream
。您发布的代码与blob或数据库无关。文件是在文件系统中找到的,而不是在数据库中。你的问题没有道理。我没有使用FileInputStream上载我需要下载的BLOB,这两个代码似乎在我的服务器中都能工作:(第一个代码允许我在我的数据库中注册一个新的记录,工作100%良好,我只需添加记录,在我的硬盘中选择file.jpg并完美地上传它。然后我可以从我的硬盘中删除file.jpg,转到datatable,在那里我可以检查我的所有记录,使用第二个方法,瞧,我把图像放回了其他目录,我想要什么就是让它在浏览器中下载。第一段代码只是上传一个文件。你还没有显示它变成Blob的部分,因此无法建议你如何再次将Blob取出。但是你声称第二段代码工作完美,所以要么你离线解决了问题,要么它不存在。你的问题似乎是与blob或数据库无关,但与如何让浏览器将内容作为文件进行处理有关。答案是内容处理标题。你引入所有这些无关的内容并错误地输入你的问题,这对你自己没有帮助。相信我,上传blob的代码工作得很好,我可以看到ev在我的数据库中的任何东西,第二个代码都不是我想要的,第二个代码只是将我的BLOB复制到一个文件夹中,我想要做的不是将它复制到一个特定的文件夹中,而是在浏览器中向用户显示一个正常的下载对话框。(我的英语不够好)BLOB是通过这个方法上载的public void generarticulo()抛出IOException{File destFile=new File(fichero.getFileName());FileUtils.copyInputStreamToFile(fichero.getInputstream(),destFile);articulo a=new articulo();a.setArchivo(destFile);a.setTitulo(titulo);a.setDescription(Description);this.controlador.registerarticulo(a);}
@WebServlet("/DownloadFileServlet")
public class DownloadFileServlet extends HttpServlet {
@Inject
ArticuloControlador controlador;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String x=request.getParameter("x");
int id = Integer.parseInt(x);
articulo a = controlador.getArticuloporID(id);
File f = a.getArchivo();
FileInputStream inStream = new FileInputStream(f);
String relativePath = getServletContext().getRealPath("");
System.out.println("relativePath = " + relativePath);
// obtengo ServletContext
ServletContext context = getServletContext();
// obtengo MIME del fichero
String mimeType= URLConnection.guessContentTypeFromName(f.getName());
if (mimeType == null) {
// steamos el MIME type si no lo encontramos
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// modificamos el response
response.setContentType(mimeType);
response.setContentLength((int) f.length());
// Descargamos
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", f.getName());
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
}
}