Tải bản đầy đủ (.pdf) (10 trang)

Chuyên đề J2EE nâng cao tuần 5

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (849.12 KB, 10 trang )

Chuyên đề J2EE nâng cao

/>
CHUYÊN ĐỀ J2EE NÂNG CAO
BÀI TẬP THỰC HÀNH TUẦN 5
UPLOAD FILE VÀ SEND EMAIL

Nội dung
1. Upload file ........................................................................................................................................... 2
2. Web mail ............................................................................................................................................. 5

/>

Chuyên đề J2EE nâng cao

/>
1. Upload file
y =”

-

/

-d



y
y

d y


y

y
d

phân.
-

-

-Trang upload.jsp

y =”

d”



y

d

upload.jsp

d

d


Chuyên đề J2EE nâng cao


-

y
d

d
d

-

/>
d

d y

d

d

upload.jsp
.

/>
d


Chuyên đề J2EE nâng cao

-


/>
y
d

d
d

d

d

)


Chuyên đề J2EE nâng cao

/>
2. Web mail
-

d

-

-

yd

d”


MailSender

/>
y

d


Chuyên đề J2EE nâng cao

-

d

/>

Chuyên đề J2EE nâng cao

/>
U
<%

int m_currentIndex=0;
String contentType = request.getContentType();
if(contentType==null && contentType.indexOf("multipart/form-data") == -1) return;
// gets length of the content data
int m_totalBytes = request.getContentLength();
//to store the contents of file in byte array
byte[] arr = new byte[m_totalBytes];

//Get data from stream
int totalRead = 0;
int readBytes = 0;

/>

Chuyên đề J2EE nâng cao

/>
for(; totalRead < m_totalBytes; totalRead += readBytes) {
request.getInputStream();
readBytes = request.getInputStream().read(arr, totalRead, m_totalBytes - totalRead);
}
/* This section will get boundary marker of upload data

*/

String m_boundary="";
boolean found = false;
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if(arr[m_currentIndex] == 13)
found = true;
else m_boundary = m_boundary + (char)arr[m_currentIndex];
m_currentIndex++;
/* This section will get dataHeader of upload data

*/

int start = m_currentIndex;
int end = 0;

found = false;
while(!found)
if(arr[m_currentIndex] == 13 && arr[m_currentIndex + 2] == 13)
{
found = true;
end = m_currentIndex - 1;
m_currentIndex = m_currentIndex + 2;
} else {
m_currentIndex++;
}


Chuyên đề J2EE nâng cao

/>
String dataHeader = new String(arr, start, (end - start) + 1);
m_currentIndex = m_currentIndex + 2;
/* This section will get dataContent until reach end of marker
int searchPos = m_currentIndex;
int keyPos = 0;
int boundaryLen = m_boundary.length();
int m_startData = m_currentIndex;
int m_endData = 0;
do
{
if(searchPos >= m_totalBytes) break;
if(arr[searchPos] == (byte)m_boundary.charAt(keyPos)){
if(keyPos == boundaryLen - 1){
m_endData = ((searchPos - boundaryLen) + 1) - 2;
break;

}
searchPos++;
keyPos++;
} else{
searchPos++;
keyPos = 0;
}
} while(true);

// Here m_startData and m_endData is what we need

/>
*/


Chuyên đề J2EE nâng cao

/>
//Get file name
String filename =dataHeader.substring(dataHeader.indexOf("filename=\"")+10);
filename = filename.substring(0,filename.indexOf("\n"));
filename = filename.substring(filename.lastIndexOf("\\")+1,filename.indexOf("\""));
String dir="C:/jakarta-tomcat-4.1.27/webapps/webmail/data/";
//Now try to write output file here
FileOutputStream fos;
fos = new FileOutputStream(dir+filename);
for (int n=m_startData; nfos.write(arr[n]);
fos.flush();
fos.close();

%>

Website lý thuyết: />Group thực hành: />


×