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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
public static void copyInputStream(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public static void deleteFile(File file) {
if (file == null)
return;
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
deleteFile(file1);
}
}
file.delete();
}
File getFile(String webpath, String storepath, String filename) {
URL url;
try {
url = new URL(webpath);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
URLConnection urlConnection;
InputStream in = null;
FileOutputStream out = null;
try {
urlConnection = url.openConnection();
urlConnection.connect();
in = urlConnection.getInputStream();
if (in == null) {
System.err.println("No connection");
return null;
}
File result = new File(storepath, filename);
out = new FileOutputStream(result);
copyInputStream(in, out);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static public boolean copier(InputStream sourceFile, OutputStream destinationFile) throws Exception {
boolean resultat = false;
// Declaration des flux
// java.io.FileInputStream sourceFile = null;
// java.io.FileOutputStream destinationFile = null;
try {
// Création du fichier :
// destination.createNewFile();
// Ouverture des flux
// sourceFile = new java.io.FileInputStream(source);
// destinationFile = new java.io.FileOutputStream(destination);
// Lecture par segment de 0.5Mo
byte buffer[] = new byte[512 * 1024];
int nbLecture;
while ((nbLecture = sourceFile.read(buffer)) != -1) {
destinationFile.write(buffer, 0, nbLecture);
}
// Copie réussie
resultat = true;
} catch (java.io.FileNotFoundException f) {
f.printStackTrace();
System.out.println("Erreur dans Copie - " + f.getMessage());
} catch (java.io.IOException e) {
System.out.println("Erreur dans Copie - " + e.getMessage());
} finally {
// Quoi qu'il arrive, on ferme les flux
try {
sourceFile.close();
} catch (Exception e) {
}
try {
destinationFile.close();
} catch (Exception e) {
}
}
return (resultat);
}
String urlServeur = (String)pageContext.getAttribute("urlServeur");
String fileExport = (String)pageContext.getAttribute("fileExport");
String xmlExport = (String)pageContext.getAttribute("urlExport");
String dirExport = (String)pageContext.getAttribute("dirExport");
String fileName ="";
String fileExtension ="";
URL url;
URLConnection urlConnection;
OutputStream fserverout;
if (fileExport == null || fileExport.length()<4){
fileExport = "event.xml";
}
// Creation des fichiers dans la zone des fichiers temporaires
// On récupère le nom du fichier puis son extension
fileName = fileExport.substring(0,fileExport.length()-4);
fileExtension = fileExport.substring(fileExport.length()-4,fileExport.length());
//Creation du fichier temporaire
File fXMLTemp = File.createTempFile(fileName,fileExtension);
OutputStream foutXML = new FileOutputStream(fXMLTemp);
foutXML.write(xmlExport.getBytes(request.getCharacterEncoding().toUpperCase()));
foutXML.close();
// fichier temporaire xml
FileInputStream finXML = new FileInputStream(fXMLTemp);
File fserver = new File(urlServeur);
if (dirExport != null && dirExport.length()>0){
urlServeur+="/"+dirExport;
}
urlServeur+="/"+fileExport;
System.out.println(urlServeur);
url = new URL(urlServeur);
try{
urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setAllowUserInteraction(true);
urlConnection.connect();
fserverout = urlConnection.getOutputStream();
//System.out.println(xmlExport);
//System.out.println(urlConnection.getURL());
copyInputStream(finXML,fserverout);
//copier(finXML,fserverout);
//fserverout.write(xmlExport.getBytes(request.getCharacterEncoding().toUpperCase()));
}
catch(Exception e){
e.printStackTrace();
} |
Partager