|
|
|
JSP - ช่วยทำให้อัพโหลด (Upload) กับ getparameter จาก Textbox สามารถเอาค่าเข้า database หน่อยครับ |
|
|
|
|
|
|
|
upload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="form1" method="post" enctype="multipart/form-data" action="testlogin2.jsp">
<p>
<input type="file" name="ImageFile" id="ImageFile" />
</p>
<input type="text" name="Latitude" id="textfield2">
<input type="text" name="Longitude" id="textfield3">
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
</body>
</html>
testlogin2
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%@ page import="javax.xml.parsers.DocumentBuilder"%>
<%@ page import="javax.xml.transform.OutputKeys"%>
<%@ page import="javax.xml.transform.Transformer"%>
<%@ page import="javax.xml.transform.TransformerFactory"%>
<%@ page import="javax.xml.transform.dom.DOMSource"%>
<%@ page import="javax.xml.transform.stream.StreamResult"%>
<%@ page import="org.w3c.dom.Attr"%>
<%@ page import="org.w3c.dom.Document"%>
<%@ page import="org.w3c.dom.Element"%>
<%@ page import="java.io.File"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.Statement"%>
<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*"%>
<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);
//out.println("file = "+file);
//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("\""));
//out.println("savefile = " + saveFile);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,
contentType.length());
//out.println("boundary = "+boundary);
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);
String savePath = "C:\\Users\\octob_000\\workspace\\Project\\WebContent\\Upload\\";
savePath = savePath.concat(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();
//Class Driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Connect Database
Connection mycon = DriverManager.getConnection("jdbc:sqlserver://JAY;databaseName=StreetView;user=sa;password=32982356");
//Query SQL
Statement stmt = mycon.createStatement();
//Insert Username and Password
String sql = "INSERT INTO image VALUES('"+savePath+"')";
stmt.executeUpdate(sql);
response.sendRedirect("insertmap.jsp");
stmt.close();
mycon.close();
}
%>
</body>
</html>
Tag : Java, Ms SQL Server 2008, JavaScript, JAVA, JSP
|
ประวัติการแก้ไข 2015-02-02 21:26:20 2015-02-02 21:27:39
|
|
|
|
|
Date :
2015-02-02 21:20:20 |
By :
jay2358 |
View :
1901 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือตอนนี้สามารถเอาเข้าดาต้าเบสได้แต่ path แต่ไม่สามารถเอาค่าจาก textbox เข้าได้ครับ
|
|
|
|
|
Date :
2015-02-02 21:23:03 |
By :
jay2358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ข้อเสียของ JSP อีกตัวหนึ่งก็คือ อันนี้แหละครรับ ยังมีรูปแบบการอ่านยากเหมือน ASP ไม่มีผิด ลองดูวิธ๊นี้ครับ
Code (JSP-Java)
FileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
// factory.setSizeThreshold(yourMaxMemorySize);
// factory.setRepository(yourTempDirectory);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload( factory );
// upload.setSizeMax(yourMaxRequestSize);
// Parse the request
List<FileItem> uploadItems = upload.parseRequest( request );
for( FileItem uploadItem : uploadItems )
{
if( uploadItem.isFormField() )
{
String fieldName = uploadItem.getFieldName();
String value = uploadItem.getString();
}
}
|
|
|
|
|
Date :
2015-02-03 09:57:09 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|