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



Clound SSD Virtual Server

JSP and Upload File Form (Java)

JSP and Upload File Form (Java) บทความนี้จะเป็นการเขียน JSP เพื่อทำการอัพโหลด Upload ไฟล์จาก Client ไปยัง Server ผ่านการ Brown ไฟล์จาก Web Browser จากนั้นทำการ Submit ไฟล์ไปยัง Server และ JSP จะทำการ Upload เพื่อ Copy ไปยัง Path ที่ต้องการ

Example ตัวอย่างการ Upload ไฟล์ด้วย JSP

JSP Upload File Form

สร้างโฟเดอร์ชื่อว่า Upload เพื่อจัดเก็บไฟล์

index.jsp
<html>
<head>
	<title>ThaiCreate.Com JSP Tutorial</title>
</head>
<body>

	<form name="frmUpload" method="post" action="upload.jsp" enctype="multipart/form-data">
		<input type="file" name="filUpload">
		<input name="btnSubmit" type="submit" value="Submit">
	</form>

</body>
</html>

upload.jsp
<%@ page import="java.io.*" %>
<html>
<head>
	<title>ThaiCreate.Com JSP Tutorial</title>
</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 and

	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;
		// creating a new file with the same name and writing the content in new file

		String savePath = application.getRealPath("\\upload\\"+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>

Output

JSP Upload File Form

เลือกไฟล์ที่จะอัพโหลด

JSP Upload File Form

อัพโหลดไฟล์เรียบร้อยแล้ว จะเห็นว่า Path จะไม่ได้อยู่ใน Workspace ที่เราเก็บ แต่จะเป็นโฟเดอร์ที่ Eclipse ทำการจำลอง Path สำหรับการรัน

JSP Upload File Form

คลิกตามโฟเดอร์จะพบไฟล์อยู่ในโฟเดอร์ของ upload






   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-08-12 18:37:19 / 2013-08-20 21:23:12
  Download : Download  JSP and Upload File Form (Java)
 Sponsored Links / Related

 
JSP and Post Request Method (Java)
Rating :

 
JSP and Get Request Method (Java)
Rating :

 
JSP and Request/Application Object (Java)
Rating :

 
JSP and Redirect sendRedirect() (Java)
Rating :

 
JSP and Auto Refresh Webpage (Java)
Rating :

 
JSP and HTML / Input form (Java)
Rating :

 
JSP and JavaScript / Ajax (Java)
Rating :

 
JSP and jQuery (Java)
Rating :

 
JSP and Ajax using jQuery (Java)
Rating :

 
JSP and Session (Create/Get/Delete) (Java)
Rating :

 
JSP and Cookies (Create/Get/Delete) (Java)
Rating :

 
JSP and JSON (Java)
Rating :

 
JSP and URLConnection (Java)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







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 อัตราราคา คลิกที่นี่