 |
|
Code (Java)
<!DOCTYPE html>
<html>
<head>
<title>:: My Upload ::</title>
<meta charset="utf-8">
</head>
<body>
<form name="myapps" action="process.jsp" method="post" enctype="multipart/form-data">
<label for="txt_file">File :</label>
<input type="file" name="txt_file" id="txt_file">
<input type="submit" name="btn_submit" id="btn_submit" value="Upload...">
</form>
</body>
</html>
process.jsp
Code (Java)
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<title>:: Upload Process... ::</title>
<meta charset="utf-8">
</head>
<body>
<%
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null
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
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
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;
//createing a new file with the same name and writing the content in new file
String savePath = application.getRealPath("\\upload\\images\\" + saveFile);
out.println("Upload file Successfully.<br>");
out.println("Save to : " + savePath);
FileOutputStream fileOut = new FileOutputStream(savePath);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}
%>
</body>
</html>
ผมสงสัยว่าผมผิดหลาดตรงไหนเหรอครับ ? ทั้งนี้ผมลอง Copy CODE ตัวอย่างแบบ เป๊ะๆแล้วก็ยังไม่ได้ครับ
ทั้งนี้ผมได้ไปตามศึกษา Class ต่างๆ ที่มีอยุ่ใน java.io ตามที่ใช้ใน CODE ชุดนี้แล้วครับก็ทำความเข้าแล้วคิดว่า CODE ไม่น่าจะผิดอะไรแต่ ประเด็นคือตัวระบบแจ้งว่า Success แต่พอเข้าไปดูใจ folder แล้วไม่พบไฟล์ที่ทำการ Upload ครับ
ผมก็เลยอยากจะถามว่าเป็นที่ permission ของ folder ของผมหรือไม่ครับ ? ผมใช้ win xp(Drive: C) ครับ
รบกวนแนะนำด้วยครับ มือใหม่ JAVA (CODE JAVA แล้วสนุกมากครับเลยอยากจะศึกษาเพิ่มเติมเรื่อยๆครับ )
Tag : Java
|
|
 |
 |
 |
 |
Date :
2014-05-22 20:20:35 |
By :
geidtiphong |
View :
1402 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |