2016-05-20 5 views
2

Ich versuche, eine Java-Web-App mit Jax-Rs zu schreiben, und ich muss einige Dateien vom Server herunterladen. Seit jetzt habe ich diese Methoden geschrieben, aber ich weiß nicht, warum das einzige whois ist die Methode, die mich das Bild herunterladen (in den anderen Fällen bekomme ich eine FileNotFoundException). Kann mir jemand erklären, warum das nicht für alle Methoden und Dateien funktioniert?Download-Dateien Jersey

Hier ist der Code ..

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.WebApplicationException; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.core.StreamingOutput; 

@Path("/download") 
public class JerseyService { 


// The file paths of the files in the server 
private static final String TXT_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\file.txt"; 
private static final String IMAGE_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\sample.png"; 
private static final String PDF_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\pdf.pdf"; 
private static final String EXCEL_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\prova.xls"; 

    /** 
* Download Text File 
*/ 
@GET 
@Path("/testtxt") 
@Produces("text/plain") 
public Response getTextFile() { 

    File file = new File(TXT_FILE); 

    Response.ResponseBuilder response = Response.ok((Object) file); 
    response.header("Content-Disposition", "attachment; filename=\"file.txt\""); 
    return response.build(); 

} 


/** 
* Download Image File 
*/ 
@GET 
@Path("/images") 
@Produces("image/png") 
public Response getImageFile() { 

    File file = new File(IMAGE_FILE); 

    Response.ResponseBuilder response = Response.ok((Object) file); 
    response.header("Content-Disposition", "attachment; filename=\"sample.png\""); 
    return response.build(); 

} 

/** 
* Download PDF File 
*/ 
@GET 
@Path("/pdf") 
@Produces("application/pdf") 
public Response getPDF() { 

    File file = new File(PDF_FILE); 

    Response.ResponseBuilder response = Response.ok((Object) file); 
    response.header("Content-Disposition", "attachment; filename=\"pdf.pdf\""); 
    return response.build(); 

} 

/** 
* Download Excel File 
*/ 
@GET 
@Path("/excel") 
@Produces("aapplication/vnd.ms-excel") 
public Response getExcell() { 

    File file = new File(EXCEL_FILE); 

    Response.ResponseBuilder response = Response.ok((Object) file); 
    response.header("Content-Disposition", "attachment; filename=\"prova.xls\""); 
    return response.build(); 

} 


@GET 
@Path("/txt") 
public Response downloadPdfFile() 
{ 
    StreamingOutput fileStream = new StreamingOutput() 
    { 
     @Override 
     public void write(java.io.OutputStream output) throws IOException, WebApplicationException 
     { 
      try 
      { 
       java.nio.file.Path path = Paths.get("C:\\Users\\POL398R\\Desktop\\file.txt"); 
       byte[] data = Files.readAllBytes(path); 
       output.write(data); 
       output.flush(); 
      } 
      catch (Exception e) 
      { 
       throw new WebApplicationException("File Not Found !!"); 
      } 
     } 
    }; 
    return Response 
      .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) 
      .header("content-disposition","attachment; filename = \"file.txt\"") 
      .build(); 
} 
} 
+0

Also sagen Sie, dass, wenn Sie schreiben, zum Beispiel, der Excel-Pfad in einem Web Browser, Sie werden nicht zum Download aufgefordert? –

+0

Er scheint zu arbeiten [dieses MKYong Tutorial] (http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/) FYI. –

+0

Genau! Wenn ich den Pfad eintippe, um das Bild herunterzuladen, funktioniert es .. aber es funktioniert nicht für die andere Art von Dateien ... – Matteo

Antwort

1

Dieses Beispiel Sie andere Arten unterstützen ändern können:

@GET 
@Path("/image") 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response getImage(
     @QueryParam("imageName") String imageName) { 

    File file = new File(imageName); 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    ImageIO.write(ImageIO.read(file), getImageExtension(imageName), baos); 

    byte[] imageData = baos.toByteArray(); 

    ResponseBuilder builder = Response.ok(imageData); 
    builder.header("Content-Disposition", "attachment; filename=" + file.getName()); 

    return builder.build(); 
} 
+0

Ja Medientyp OCTET-Stream funktioniert besser als zip. Dateiname mit Zip ist genug – tgkprog