Pada aplikasi web, terkadang kita membutuhkan suatu interface yang bisa digunakan user untuk mengupload file ke server. File upload adalah kegiatan menambah atau menyalin file ke dalam suatu system komputer dengan cara memindahkan file yang lokasinya di komputer lokal ke komputer remote. File upload sering kali kita jumpai dalam suatu aplikasi web. Sebagai contohnya aplikasi web-mail, menggunakan file upload sebagai cara untuk melampirkan lampiran/attachment ketika user yang mempunyai account dalam web-mail tersebut ingin mengirimkan email dengan attachment.
Pada aplikasi web JSP, file upload mempunyai perlakuan tersendiri dalam prosesnya. Sebab, pada konsepnya file upload mempunyai proses yang lumayan rumit. Meskipun sudah banyak pustaka/library java yang mempermudah kita untuk membuat mekanisme file upload, namun tidak sedikit juga programmer yang membuat mekanisme file upload dengan caranya sendiri.
Berikut adalah sintaks yang digunakan untuk membuat fitur file upload:
index.jsp
Download: File Upload di JSP.pdf
Berikut adalah sintaks yang digunakan untuk membuat fitur file upload:
index.jsp
<HTML>
<HEAD>
<TITLE>Form Upload</TITLE>
</HEAD>
<BODY>
<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>FILE UPLOAD</B>
<center>
</td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>Pilih file yang ingin diupload:</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>
</BODY>
</HTML>
upload.jsp<HEAD>
<TITLE>Form Upload</TITLE>
</HEAD>
<BODY>
<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>FILE UPLOAD</B>
<center>
</td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>Pilih file yang ingin diupload:</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>
</BODY>
</HTML>
<%@ page import="java.io.*"%>
<%
String nameFile = "";
String saveFile = "";
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);
nameFile = file.substring(file.indexOf("filename=\"") + 10);
nameFile = nameFile.substring(0, nameFile.indexOf("\n"));
nameFile = nameFile.substring(nameFile.lastIndexOf("\\") + 1, nameFile.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;
saveFile = "C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/jsp/upload/" + nameFile;
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>Kamu berhasil mengupload file dengan nama: </b>
<%
out.println(saveFile);
}
%>
</td>
</tr>
</table>
<%
String nameFile = "";
String saveFile = "";
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);
nameFile = file.substring(file.indexOf("filename=\"") + 10);
nameFile = nameFile.substring(0, nameFile.indexOf("\n"));
nameFile = nameFile.substring(nameFile.lastIndexOf("\\") + 1, nameFile.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;
saveFile = "C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/jsp/upload/" + nameFile;
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>Kamu berhasil mengupload file dengan nama: </b>
<%
out.println(saveFile);
}
%>
</td>
</tr>
</table>
Download: File Upload di JSP.pdf
0 Comments
Post a Comment