Java - OutputStream.write() con Archivos grandes

 
Vista:
Imágen de perfil de Luis
Val: 7
Ha aumentado su posición en 5 puestos en Java (en relación al último mes)
Gráfica de Java

OutputStream.write() con Archivos grandes

Publicado por Luis (4 intervenciones) el 17/08/2019 08:32:59
Hola Estoy creando un servidor web con la clase HTTPServer, la idea es crear un servidor web con el cual puedas descargar archivos.
Hasta el momento el código funciona correctamente a excepción de que los archivos grandes como por ejemplo de 4gb no me los descarga.
Se que estos archivos superan el máximo de Integer.MAX_VALUE pero no se como adaptar este código para poder descargar estos archivos.
Esta es la parte del código que se encarga de hacer la descarga.

Al igual les dejo el repositorio completo en GitHub por si gustan verlo.
Entorno de desarrollo: Netbeans 8.2
https://github.com/LuisAcxis/ServidorWeb.git

Gracias por la ayuda

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
File file = new File (Nombre);
 
byte[] bytes  = new byte [(int)file.length()];
 
Headers headers = he.getResponseHeaders();
headers.add("Server", "Luis Acxis 1.0");
headers.add("Date", String.valueOf(new Date()));
headers.add("Type", Tipo);
headers.add("Content-Length", String.valueOf(file.length()));
headers.add("Content-Type", "application/octet-stream "+Tipo);
 
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
 
he.sendResponseHeaders(responseCode_OK, file.length());
OutputStream outputStream = he.getResponseBody();
 
int count;
while ((count = bufferedInputStream.read(bytes)) > 0)
{
    outputStream.write(bytes, 0, count);
}
 
outputStream.close();
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Luis
Val: 7
Ha aumentado su posición en 5 puestos en Java (en relación al último mes)
Gráfica de Java

OutputStream.write() con Archivos grandes

Publicado por Luis (4 intervenciones) el 19/08/2019 10:28:35
Al final ya logre solucionarlo, el código quedo así por si a alguien le interesa.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
File file = new File (Nombre);
Headers headers = he.getResponseHeaders();
headers.add("Server", "Luis Acxis 1.0");
headers.add("Date", String.valueOf(new Date()));
headers.add("Type", Tipo);
headers.add("Content-Length", String.valueOf(file.length()));
headers.add("Content-Type", "application/octet-stream "+Tipo);
 
 
FileInputStream archivo_lectura = new FileInputStream(file);
BufferedInputStream archivo_buffered = new BufferedInputStream(archivo_lectura);
he.sendResponseHeaders(responseCode_OK, file.length());
OutputStream outputStream = he.getResponseBody();
 
int max = 4096;
int pos = 0;
while(pos < file.length())
{
    int tamaño = 0;
    if(file.length() > max)
    {
        if((file.length() - pos) > max)
        {
            tamaño = max;
            pos += max;
        }
        else
        {
            tamaño = (int)file.length() - pos;
            pos += tamaño;
        }
    }
    else
    {
        tamaño = (int)file.length();
        pos = (int)file.length();
    }
 
    byte bytes[] = new byte[tamaño];
    archivo_buffered.read(bytes, 0, tamaño);
    outputStream.write(bytes, 0, tamaño);
}
 
outputStream.close();
archivo_lectura.close();
archivo_buffered.close();
 
System.out.println("Transferencia completa");
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar