上传文件及将大数据写入数据库并通过页面访问.docx
《上传文件及将大数据写入数据库并通过页面访问.docx》由会员分享,可在线阅读,更多相关《上传文件及将大数据写入数据库并通过页面访问.docx(13页珍藏版)》请在冰豆网上搜索。
上传文件及将大数据写入数据库并通过页面访问
对于如下页面:
<%@pagecontentType="text/html;charset=GBK"%>
文件上载
歌手名:
歌手照片:
作者名:
歌曲:
歌曲描述:
通过浏览器传输时,所传输的数据的格式为:
-----------------------------7d8b07a02ce
Content-Disposition:
form-data;name="singer"
站三
-----------------------------7d8b07a02ce
Content-Disposition:
form-data;name="photo";filename="E:
\Temp\file1.txt"
Content-Type:
text/plain
AAAAAAAAAAAAAA
AAAAAAAAAAAAAA
AAAAAAAAAAAAAA
-----------------------------7d8b07a02ce
Content-Disposition:
form-data;name="composer"
李四
-----------------------------7d8b07a02ce
Content-Disposition:
form-data;name="song";filename="E:
\Temp\file2.txt"
Content-Type:
text/plain
BBBBBBBBBBBBBB
BBBBBBBBBBBBBB
BBBBBBBBBBBBBB
-----------------------------7d8b07a02ce
Content-Disposition:
form-data;name="memo"
好感
-----------------------------7d8b07a02ce--
浏览器会以特定的“节”标记来分割每一个数据区,本例的特定分割符为“-----------------------------7d8b07a02ce”。
并且对传输的任何数据,浏览器均会以“回车换行”符结束。
基于此,我们可以用如下的Servlet程序获取以上表单数据:
packagecom.ttt.chapter8;
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjavax.servlet.ServletInputStream;
importjava.sql.*;
/**
*ServletimplementationclassforServlet:
UpLoad
*
*/
publicclassUpLoadextendsjavax.servlet.http.HttpServletimplementsjavax.servlet.Servlet{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
Stringsinger;
Stringcomposer;
Stringsong;
byte[]buff=newbyte[1024];
StringsectionDelimeter;
/*(non-Java-doc)
*@seejavax.servlet.http.HttpServlet#HttpServlet()
*/
publicUpLoad(){
super();
}
/*(non-Java-doc)
*@seejavax.servlet.http.HttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)
*/
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
}
/*(non-Java-doc)
*@seejavax.servlet.http.HttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)
*/
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
intcount;
ServletInputStreamsis=request.getInputStream();
//skipthebeginning3lines,thengetthe4thlinewhichissinger
//注意:
用sis.readLine(buff,0,buff.length)读入的内容包含结尾的“回车换行”符
count=sis.readLine(buff,0,buff.length);
sectionDelimeter=newString(buff,0,count-2);
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
singer=newString(buff,0,count-2);
System.out.println("Singer="+singer);
//skipthebeginning4lines,thengetthesingerphotoimagestream
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
intrc1;
ByteArrayOutputStreamphoto=newByteArrayOutputStream();
while(true)
{
rc1=sis.readLine(buff,0,buff.length);
if(rc1==buff.length)
{
photo.write(buff,0,rc1);
}
else
{
Stringdel=newString(buff,0,rc1);
if(del.indexOf(sectionDelimeter)!
=-1)
break;
else
{
photo.write(buff,0,rc1);
}
}
}
System.out.println("Photosize="+photo.size());
/*
FileOutputStreamfos=newFileOutputStream("E:
\\Temp\\ABC.jpg");
System.out.println("Size="+photo.size());
fos.write(photo.toByteArray());
fos.flush();
fos.close();
*/
//getcomposer
//因为我们在前一个对图像媒体的读取中,已经读出了域分隔符,所以,此处只要读两个。
sis.readLine(buff,0,buff.length);
sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
composer=newString(buff,0,count-2);
System.out.println("Composer="+composer);
//getthesongmedia
//skipthebeginning4lines,thengetthesingerphotoimagestream
Stringss;
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
ss=newString(buff,0,count-2);
StringsongName=ss.substring(ss.lastIndexOf("\\")+1,ss.length()-1);
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
intrc2;
ByteArrayOutputStreamsong=newByteArrayOutputStream();
while(true)
{
rc2=sis.readLine(buff,0,buff.length);
if(rc2==buff.length)
{
song.write(buff,0,rc2);
}
else
{
Stringdel=newString(buff,0,rc2);
if(del.indexOf(sectionDelimeter)!
=-1)
break;
else
{
song.write(buff,0,rc2);
}
}
}
System.out.println("SongName="+songName+"Size="+song.size());
/*
FileOutputStreamfos=newFileOutputStream("E:
\\Temp\\ABC.mp3");
System.out.println("Size="+song.size());
fos.write(song.toByteArray());
fos.flush();
fos.close();
*/
//getthesongmemo
//因为我们在前一个对图像媒体的读取中,已经读出了域分隔符,所以,此处只要读两个。
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
count=sis.readLine(buff,0,buff.length);
Stringmemo=newString(buff,0,count-2);
System.out.println("Memo="+memo);
//insertthemediaintotable
intrest=0;
rest=insertIntoDatabase(singer,photo,composer,song,memo,songName);
response.setContentType("text/html");
PrintWriterpw=response.getWriter();
if(rest>0)
{
pw.println("Success");
}
else
{
pw.println("Failed");
}
}
privateintinsertIntoDatabase(Stringsinger,ByteArrayOutputStreamphoto,Stringcomposer,ByteArrayOutputStreamsong,Stringmemo,StringsongName)
{
Connectionconn=null;
intca=0;
PreparedStatementpst=null;
StringconnectionUrl="jdbc:
sqlserver:
//192.168.10.80:
1433;"+
"databaseName=Student;user=sa;password=12345";
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(connectionUrl);
pst=conn.prepareStatement("insertintoSong(song_singer_name,song_singer_photo,song_composer,song_singer_song,song_memo,song_name)values(?
?
?
?
?
?
)");
pst.setString(1,singer);
pst.setBytes(2,photo.toByteArray());
pst.setString(3,composer);
pst.setBytes(4,song.toByteArray());
pst.setString(5,memo);
pst.setString(6,songName);
ca=pst.executeUpdate();
}
catch(Exceptione)
{
e.printStackTrace();
}
finally
{
try
{
pst.close();
conn.close();
}
catch(Exceptione1){};
}
returnca;
}
}
现在已经将音乐数据存入了数据库,可以通过如下的页面和Servlet来播放:
播放页面:
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:
//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
//www.w3.org/1999/xhtml">
媒体播放测试--使用Microsoftmediaplayer播放媒体,可以播放MP3、MPEG等-->
--
6BF52A52-394A-11d3-B153-00C04F79FAA6"codebase="align="baseline"border="0"standby="LoadingMicrosoftWindowsMediaPlayercomponents..."type="application/x-oleobject">
-->
--使用Realplayer播放媒体,可以播放几乎任何类型的媒体,包括mp3,RM等-->
--
CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"WIDTH=292HEIGHT=294>
-->
--使用默认的Player播放媒体-->