grjoseph
Messages postés3Date d'inscriptionjeudi 26 mars 2009StatutMembreDernière intervention30 mai 2010
-
30 mai 2010 à 17:47
grjoseph
Messages postés3Date d'inscriptionjeudi 26 mars 2009StatutMembreDernière intervention30 mai 2010
-
30 mai 2010 à 17:50
Voila j le code suivant
import com.oreilly.servlet.MultipartRequest;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadPhoto extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException , IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String rtempfile = File.createTempFile("temp","1").getParent();
MultipartRequest multi = new MultipartRequest(req, rtempfile, 500 * 1024);
File rnewfile=null;
rnewfile = new File(new File("/").getAbsolutePath()+File.separator+"tomcat"+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"Photo"+File.separator+"YourPhotoname.jpg");
out.println("<HTML>");
out.println("<head><title>UPLOAD PHOTO</title></head>");
out.println("");
out.println("");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
File f = multi.getFile(name);
FileInputStream fin =new FileInputStream(f);
ImageInfo ii =new ImageInfo();
ii.setInput(fin);
fin.close();
fin =new FileInputStream(f);
FileOutPutStream fos =new FileOutPutStream(rnewfile);
byte sizefile[] = new byte[500000];
fin.read(sizefile);
fin.write(sizefile);
fos.close();
fin.close();
f.delete();
}
res.sendRedirect("Your.jsp");
}
}
J'arrive pas a changer le dossier de récupération des fichiers uploaded je trouve tjrs les fichiers dans le dossiers d'éclipse
Merci d'avance.
grjoseph
Messages postés3Date d'inscriptionjeudi 26 mars 2009StatutMembreDernière intervention30 mai 2010 30 mai 2010 à 17:50
<%@ page import="java.io.*" %>
<%
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.
getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
// File savedFile = new File(config.getServletContext().getRealPath("/")+"emp_image\\image\"+itemName);
String saveFile = file.substring(file.indexOf("filename="") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\r"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\")+ 1,saveFile.indexOf("""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
//extracting the index of file
pos = file.indexOf("filename="");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
You have successfully
upload the file by the name of:
<% out.println(saveFile);%>
<%
}
%>
ce code est plus clair
si quelqu'un peut m'aider a changer les dossier de récupération des fichier uploaded.
mercii.