|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2001
Messaggi: 9947
|
[JSP] File upload: qualcuno lo sa fare?!
In PHP son riuscito a capire come farlo, ma dovrei implementarlo in JSP.
__________________
Aiuta la ricerca col tuo PC: >>Calcolo distribuito BOINC.Italy: unisciti anche tu<< Più largo è il sorriso, più affilato è il coltello. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Aug 2004
Città: Salento
Messaggi: 1080
|
Ho fatto una rapida ricerca su google, prova questo
Ultima modifica di DigitalKiller : 09-07-2010 alle 17:43. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Dec 2001
Città: Milano
Messaggi: 545
|
vedi anche Jakarta Commons FileUpload
__________________
Angus the Hunter @ Realm of magic | Angus Young @ Batracer °SetiEmperor°| Ninja Technologies { qualunque cosa sia, è veloce e fa male (cit.) } |
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Jul 2001
Messaggi: 9947
|
Quote:
Codice:
Hello Guys,
I have tried this example and successfully load the file on the server.
-----------------Form.html-------------------------------------------
<form action="upload.jsp" method="POST" enctype="multipart/form-data">
<!-- enctype="multipart/form-data" -->
<input type="file" name="theFile"><br>
<input type="submit">
</form>
------------------------------ upload.jsp --------------------------------------------
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos;
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;
saveFile = "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\ROOT\\Extrusions\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
%>
As you can see the path in Extrusions this is the path where I can save the files.
It will work, I am sure please try as it is.
Raheel Rehman
__________________
Aiuta la ricerca col tuo PC: >>Calcolo distribuito BOINC.Italy: unisciti anche tu<< Più largo è il sorriso, più affilato è il coltello. |
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jul 2001
Messaggi: 9947
|
Grazie, + avanti ritornonerò su questo argmento, ora devo portare avanti un'altra faccenda
__________________
Aiuta la ricerca col tuo PC: >>Calcolo distribuito BOINC.Italy: unisciti anche tu<< Più largo è il sorriso, più affilato è il coltello. |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jul 2001
Messaggi: 9947
|
Oppure questo bean:
Codice:
//ÎļþÄÚÈÝ
i = in.readLine(line, 0, 1280);
setContentType(new String(line, 0, i-2));
i = in.readLine(line, 0, 1280);
//¿ÕÐÐ
i = in.readLine(line, 0, 1280);
newLine = new String(line, 0, i,"ISO8859_1");
FileOutputStream pw=new FileOutputStream((savePath==null? "" : savePath) + filename);
//PrintWriter pw = new PrintWriter(new BufferedWriter(new
//FileWriter((savePath==null? "" : savePath) + filename)));
while (i != -1 && !newLine.startsWith(boundary)) {
// ÎļþÄÚÈݵÄ×îºóÒ»Ðаüº¬»»ÐÐ×Ö·û
// Òò´ËÎÒÃDZØÐë¼ì²éµ±Ç°ÐÐÊÇ·ñÊÇ×î
// ºóÒ»ÐÐ
i = in.readLine(line, 0, 1280);
if ((i==boundaryLength+2 || i==boundaryLength+4) && (new String(line, 0, i).startsWith(boundary)))
pw.write(newLine.substring(0, newLine.length()-2).getBytes("ISO8859_1"));
else
pw.write(newLine.getBytes("ISO8859_1"));
newLine = new String(line, 0, i,"ISO8859_1");
}
pw.close();
}
else {
// ÆÕͨ±íµ¥ÊäÈëÔªËØ
// »ñÈ¡ÊäÈëÔªËØÃû×Ö
int pos = newLine.indexOf("name=\"");
String fieldName = newLine.substring(pos+6, newLine.length()-3);
i = in.readLine(line, 0, 1280);
i = in.readLine(line, 0, 1280);
newLine = new String(line, 0, i);
StringBuffer fieldValue = new StringBuffer(1280);
while (i != -1 && !newLine.startsWith(boundary)) {
// ×îºóÒ»Ðаüº¬»»ÐÐ×Ö·û
// Òò´ËÎÒÃDZØÐë¼ì²éµ±Ç°ÐÐÊÇ·ñÊÇ×îºóÒ»ÐÐ
i = in.readLine(line, 0, 1280);
if ((i==boundaryLength+2 || i==boundaryLength+4) && (new String(line, 0, i).startsWith(boundary)))
fieldValue.append(newLine.substring(0, newLine.length()-2));
else
fieldValue.append(newLine);
newLine = new String(line, 0, i);
}
fields.put(fieldName, fieldValue.toString());
}
}
i = in.readLine(line, 0, 1280);
}
}
}
--jsp1.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<jsp:useBean id="TheBean" scope="page" class="FileUploadBean.FileUploadBean" />
<%
TheBean.setSavePath("d:\\");
TheBean.doUpload(request);
out.println("Filename:" + TheBean.getFilename());
out.println("<BR>ContentType:" + TheBean.getContentType());
out.println("<BR>Author:" + TheBean.getFieldValue("author"));
out.println("<BR>Company:" + TheBean.getFieldValue("company"));
out.println("<BR>Comment:" + TheBean.getFieldValue("comment"));
%>
-- jload.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>File Upload Demo </title>
</head>
<body>
<form action=jsp1.jsp enctype="MULTIPART/FORM-DATA" method=post>
Author: <input type=text name=author>
<br>
Company: <input type=text name=company>
<br>
Comment : <textarea name=comment></textarea>
<br>
Choose the upload file <input type=file name=filename>
<br>
File description : <input type=text name=description>
<br>
<input type=submit value="Upload">
</form>
</body>
</html>
__________________
Aiuta la ricerca col tuo PC: >>Calcolo distribuito BOINC.Italy: unisciti anche tu<< Più largo è il sorriso, più affilato è il coltello. |
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jul 2001
Messaggi: 9947
|
Non li ho ancora provati, forse non li uso, ma se li provo cerco di ricordare d'aggiornare questo 3D per i posteri.
3D inerenti: [JSP/SERVLET] Upload di file su WEB
__________________
Aiuta la ricerca col tuo PC: >>Calcolo distribuito BOINC.Italy: unisciti anche tu<< Più largo è il sorriso, più affilato è il coltello. Ultima modifica di Matrixbob : 03-09-2005 alle 15:01. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:01.



















