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
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Request Parameters Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3></h3>");
out.println("<br>");
// doGet(req,resp);
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if(!isMultipart){
req.setAttribute("error","Request was not multipart!" );
out.println(" error,Request was not multipart!");
} else{
File yourTempDirectory= new File ("../WebContent/WEB-INF/dtd/titi.odt");
int yourMaxMemorySize=1000;
int yourMaxRequestSize=400000;//4Mo
out.println(" ok,Request was not multipart!");
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory(
yourMaxMemorySize, yourTempDirectory);
//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//Set overall request size constraint
upload.setSizeMax(yourMaxRequestSize);
out.println("FormField Servelty ");
// Parse the request
try {
out.println("FormField TRY ");
List /* FileItem */ fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
out.println("FormField avant while ");
while (iter.hasNext()) {
out.println("FormField apres while ");
FileItem fi = (FileItem) iter.next();
if (fi.isFormField()) {
//Champ "normal" de formulaire
out.println("FormField "+ fi.getName());
}
else { //fileItemsfichier
out.println("FormField est file ");
// On recupere le nom du fichier
String nomDufichier= fi.getName().toString();
if (nomDufichier != null) {
// taille du nom
int taille = nomDufichier.length();
// fin du nom
int finDuChemin = nomDufichier.lastIndexOf("\\");
// on crée le bon nom du fichier avec l'extension
nomDufichier = nomDufichier.substring(finDuChemin + 1,
taille);
// On ecrit le fichier sur le disque
String adressedestockage = "../WebContent/WEB-INF/dtd/";
// Upload du fichier
File uploadedFile = new File(adressedestockage);
//ecriture du fichier
fi.write(uploadedFile);
}
}}
} catch (FileUploadException e) {
out.println("FormField FileUploadException ");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
out.println("FormField Exception ");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Partager