v_upload.jsp
<FORM ENCTYPE="multipart/form-data"
ACTION="upload.jsp" METHOD=POST>
<br>
<br>
<br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td
colspan="2" align="center"><b>UPLOAD THE
FILE</b>
<center>
</td>
</tr>
<tr>
<td
colspan="2" align="center"></td>
</tr>
<tr>
<td><b>File yang akan di upload :</b></td>
<td><INPUT NAME="file"
TYPE="file"></td>
</tr>
<tr>
<td
colspan="2" align="center"></td>
</tr>
<tr>
<td
colspan="2" align="center"><input
type="submit"
value="Send File"></td>
</tr>
<table>
</center>
</form>
upload.jsp
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String
saveFile = "";
String
nmaFile = "";
int
TotnamaFile = 0;
String
contentType = request.getContentType();
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);
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;
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;
String
data = "D:/IMPORTANTS FILES/NetBeans/Web2015/web/images/";
saveFile = data + saveFile;
TotnamaFile = saveFile.length();
nmaFile=saveFile.substring(data.length(),TotnamaFile);
File ff
= new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<br>
<table border="2">
<tr>
<td><b>Berhasil di upload :</b>
<% out.println(saveFile); }
%>
<hr><img src="images/<% out.print(nmaFile);
%>"><hr><%
out.print(TotnamaFile); %>
<hr><% out.print(nmaFile);
%>
</td>
</tr></table>
</body>
</html>
No comments:
Post a Comment