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
|
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TDF extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
// On ouvre le flux et on commence la récupération du fichier
ServletInputStream sis = request.getInputStream();
int len = request.getContentLength();
byte[] buff = new byte[len];
BufferedInputStream in = new BufferedInputStream(sis, buff.length);
try {
createFile(in);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int createFile(BufferedInputStream in) throws Exception {
int count = 0;
File tmpFile = new File("C:/Users/olive/Desktop/tmp", "exemple.html");
BufferedOutputStream out = null;
FileOutputStream fos = null;
try
{
if (tmpFile.exists()) tmpFile.delete();
tmpFile.createNewFile();
fos = new FileOutputStream(tmpFile);
byte[] buff = new byte[8192];
out = new BufferedOutputStream(fos, buff.length);
int i;
while ((i = in.read(buff,0,buff.length)) != -1)
{
out.write(buff,0,i);
count += i;
/*
String nulle = null;
nulle.toLowerCase();
*/
}
tmpFile = null;
out.close();
fos.close();
}
catch(Exception e)
{
try {
out.close();
fos.close();
}
catch (IOException e1)
{
throw e1;
}
if(tmpFile != null && tmpFile.exists()) tmpFile.delete();
throw e;
}
return count;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
//on passe la main au Get
doGet(request, response);
}
} |
Partager