Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
java - descargar un flujo de entrada usando servlets

Estoy usando el siguiente código para obtener el contenido de un objeto en el depósito s3. Puedo copiar los datos en un archivo localmente, pero el archivo debe 'descargarse' y debe mostrarse en la lista de descargas del navegador. Busqué un poco sobre esto y pude saber que esto tiene algo que ver con response.setHeader( "Content-Disposition", "attachment;filename=\"" + filename + "\"" ); Intenté agregar eso también, pero de alguna manera no pude hacerlo funcionar. Tengo entendido que la fuente tiene que ser un archivo en un servidor que se convierte en una transmisión. pero obtengo el contenido de s3 en forma de flujo de entrada. ¿Cómo descargo esto como un archivo en el navegador? A continuación se muestra el código que he intentado hasta ahora

 AmazonS3 s3 = new AmazonS3Client(new ProfileCredentialsProvider()); S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketname, fileloc)); final BufferedInputStream i = new BufferedInputStream(fetchFile.getObjectContent()); response.setContentType("application/json"); response.setHeader( "Content-Disposition", "attachment;filename=\"" + filename + "\"" ); ServletOutputStream sos = response.getOutputStream(); int bytesread = i.read(); while(bytesread!=-1) { sos.write(bytesread); bytesread = i.read(); } if(i!= null)i.close(); if(sos!= null)sos.close();
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Todo es correcto excepto la parte de lectura del archivo.

 //Set the size of buffer to stream the data. byte []buffer=new byte[1024*8];

en vez de

 int byte=i.read();

Ahora lee el archivo.

 while( ( length = yourInputStream.read(buffer))!=-1) { yourOutputStream.write(buffer); } System.out.println("File is downloaded.");

Además, colocar todo el código dentro del bloque try/catch lo ayudará a conocer el motivo exacto de su problema.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!