Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java Forum > JSP - [ขอความช่วยเหลือครับ] : เรื่องการ Upload File จาก Form HTML ครับ



 

JSP - [ขอความช่วยเหลือครับ] : เรื่องการ Upload File จาก Form HTML ครับ

 



Topic : 108576



โพสกระทู้ ( 889 )
บทความ ( 11 )



สถานะออฟไลน์
Facebook Blogger



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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-05-22 20:20:35 By : geidtiphong View : 1290 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook



ดูจากโฟเดอร์ที่มันชี้ครับ น่าจะเข้าไปอยู่ในนี้ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-05-23 09:36:27 By : mr.win
 


 

No. 2



โพสกระทู้ ( 889 )
บทความ ( 11 )



สถานะออฟไลน์
Facebook Blogger

อ่อ จริงๆก้วยครับพี่วินครับพอดีผมเข้าไปดูแค่ C:\javaworkspace\upload\images เลยไม่เจอไฟล์ครับ ตกม้าตายอีกแล้วครับไม่สังเกตุ path ให้ดีๆก่อนถามครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-05-23 13:24:48 By : geidtiphong
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-05-23 17:19:22 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : JSP - [ขอความช่วยเหลือครับ] : เรื่องการ Upload File จาก Form HTML ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่