I am uploading files to a server through php and java. I have a java class that helps me with this using Post. I can upload files correctly and access them but sometimes they contain accents and special characters that the Apache server does not recognize and therefore ends up changing its name. This causes that when searching for the file using the name stored in the database, it cannot be accessed.
This is the original name and the result on the server:
- wings number 1.jpeg
- wings number 1.jpeg
To avoid this I want to modify the name of the file to upload from java with:
URLEncoder.encode("alas número 1.jpeg", "UTF-8");
This way I would no longer have problems with my files. The bottom line is that I can't rename the file with File without creating another file or I haven't been able to find a way to do it.
Basically what I do is this:
public class FicherosOperaciones {
/**
* Duplica el fichero que se le pase y lo retorna como parametro.
*/
public static File duplicar(File ficheroACopiar){
FileInputStream in = null;
int aleatorio = (int) (Math.random()*2);
File ficheroDestino = new File(ficheroACopiar.getAbsolutePath()+aleatorio);
try {
in = new FileInputStream(ficheroACopiar);
FileOutputStream out = new FileOutputStream(ficheroDestino);
int c;
while ((c = in.read())!= -1) {
out.write(c);
}
in.close();
out.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(FicherosOperaciones.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FicherosOperaciones.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(FicherosOperaciones.class.getName()).log(Level.SEVERE, null, ex);
}
}
//ficheroDestino.deleteOnExit();
return ficheroDestino;
}
/**
* Duplica un fichero y lo renombra para que coincida con UTF-8.
* @param fichero
* @return
*/
public static File duplicarUTF8(File fichero){
File duplicado=null;
try {
String ruta = fichero.getParent();
String nombreUTF8 = URLEncoder.encode(fichero.getName(), "UTF-8");
File ficheroRenombrado = new File(ruta+System.getProperty("file.separator")+nombreUTF8);
duplicado = duplicar(fichero);
duplicado.renameTo(ficheroRenombrado);
return ficheroRenombrado;
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(FicherosOperaciones.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FicherosOperaciones.class.getName()).log(Level.SEVERE, null, ex);
}
return duplicado;
}
}
The code duplicates the file, renames it, and then returns it for use. My plan is to upload it to the server and delete it. But the copying process is very slow if I have to upload 10 or 15 files. Is there any other way to make it faster?
I don't want to lose the original file or modify it, but when uploading to the server I want it to have the new name.
It's slow because the algorithm you use copies byte by byte from one file to another, which makes the process slow. This is noted here:
Instead of copying 1 to 1, what you can do is copy a chunk of bytes, usually 4KBs or 8KBs.
BufferedInputStream
You can achieve this using the and classesBufferedOutputStream
:If you're using Java 7 or higher, you can also choose to use
Files#copy
which uses a similar strategy.