1、上传文件及将大数据写入数据库并通过页面访问对于如下页面: 文件上载 歌手名: 歌手照片: 作者名: 歌曲: 歌曲描述: 通过浏览器传输时,所传输的数据的格式为:-7d8b07a02ceContent-Disposition: form-data; name=singer站三-7d8b07a02ceContent-Disposition: form-data; name=photo; filename=E:Tempfile1.txtContent-Type: text/plainAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-7d8b07a02ceConte
2、nt-Disposition: form-data; name=composer李四-7d8b07a02ceContent-Disposition: form-data; name=song; filename=E:Tempfile2.txtContent-Type: text/plainBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-7d8b07a02ceContent-Disposition: form-data; name=memo好感-7d8b07a02ce-浏览器会以特定的“节”标记来分割每一个数据区,本例的特定分割符为“-7d8b07a02ce
3、”。并且对传输的任何数据,浏览器均会以“回车换行”符结束。基于此,我们可以用如下的Servlet程序获取以上表单数据:package com.ttt.chapter8;import java.io.*; import javax.servlet.*; import javax.servlet.http.*;import javax.servlet.ServletInputStream;import java.sql.*;/* * Servlet implementation class for Servlet: UpLoad * */ public class UpLoad extends j
4、avax.servlet.http.HttpServlet implements javax.servlet.Servlet /* * */ private static final long serialVersionUID = 1L; String singer; String composer; String song; byte buff = new byte1024; String sectionDelimeter; /* (non-Java-doc) * see javax.servlet.http.HttpServlet#HttpServlet() */ public UpLoa
5、d() super(); /* (non-Java-doc) * see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stub /* (non-Java-do
6、c) * see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stub int count; ServletInputStream sis = reque
7、st.getInputStream(); /skip the beginning 3 lines, then get the 4th line which is singer /注意:用sis.readLine(buff, 0, buff.length)读入的内容包含结尾的“回车换行”符 count = sis.readLine(buff, 0, buff.length); sectionDelimeter = new String(buff, 0, count-2); count = sis.readLine(buff, 0, buff.length); count = sis.readLi
8、ne(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length); singer = new String(buff, 0, count-2); System.out.println(Singer= + singer); /skip the beginning 4 lines, then get the singer photo image stream count = sis.readLine(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length
9、); count = sis.readLine(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length); int rc1; ByteArrayOutputStream photo = new ByteArrayOutputStream(); while(true) rc1 = sis.readLine(buff, 0, buff.length); if (rc1 = buff.length) photo.write(buff, 0, rc1); else String del = new String(buff, 0,
10、 rc1); if (del.indexOf(sectionDelimeter) != -1) break; else photo.write(buff, 0, rc1); System.out.println(Photo size= + photo.size(); /* FileOutputStream fos = new FileOutputStream(E:TempABC.jpg); System.out.println(Size=+photo.size(); fos.write(photo.toByteArray(); fos.flush(); fos.close(); */ /get
11、 composer /因为我们在前一个对图像媒体的读取中,已经读出了域分隔符,所以,此处只要读两个。 sis.readLine(buff, 0, buff.length); sis.readLine(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length); composer = new String(buff, 0, count-2); System.out.println(Composer= + composer); /get the song media /skip the beginning 4 lines, t
12、hen get the singer photo image stream String ss; count = sis.readLine(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length); ss = new String(buff, 0, count-2); String songName = ss.substring(ss.lastIndexOf()+1, ss.length()-1); count = sis.readLine(buff, 0, buff.length); count = sis.readL
13、ine(buff, 0, buff.length); int rc2; ByteArrayOutputStream song = new ByteArrayOutputStream(); while(true) rc2 = sis.readLine(buff, 0, buff.length); if (rc2 = buff.length) song.write(buff, 0, rc2); else String del = new String(buff, 0, rc2); if (del.indexOf(sectionDelimeter) != -1) break; else song.w
14、rite(buff, 0, rc2); System.out.println(Song Name= + songName + Size = + song.size(); /* FileOutputStream fos = new FileOutputStream(E:TempABC.mp3); System.out.println(Size=+song.size(); fos.write(song.toByteArray(); fos.flush(); fos.close(); */ /get the song memo /因为我们在前一个对图像媒体的读取中,已经读出了域分隔符,所以,此处只要
15、读两个。 count = sis.readLine(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length); count = sis.readLine(buff, 0, buff.length); String memo = new String(buff, 0, count-2); System.out.println(Memo = + memo); /insert the media into table int rest = 0; rest = insertIntoDatabase(singer, photo,
16、composer, song, memo, songName); response.setContentType(text/html); PrintWriter pw = response.getWriter(); if (rest 0) pw.println(Success); else pw.println(Failed); private int insertIntoDatabase(String singer, ByteArrayOutputStream photo, String composer, ByteArrayOutputStream song, String memo, S
17、tring songName) Connection conn = null; int ca = 0; PreparedStatement pst = null; String connectionUrl = jdbc:sqlserver:/192.168.10.80:1433; + databaseName=Student;user=sa;password=12345; try Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver); conn = DriverManager.getConnection(connectionUr
18、l); pst = conn.prepareStatement(insert into Song (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.setStr
19、ing(5, memo); pst.setString(6, songName); ca = pst.executeUpdate(); catch(Exception e) e.printStackTrace(); finally try pst.close(); conn.close(); catch(Exception e1); return ca; 现在已经将音乐数据存入了数据库,可以通过如下的页面和Servlet来播放:播放页面:媒体播放测试!- -!- - 其中的“./PlayMusic”是一个Servlet,代码如下:package com.ttt.chapter8;import
20、java.io.*; import javax.servlet.*; import javax.servlet.http.*;import java.sql.*;/* * Servlet implementation class for Servlet: PlayMusic * */ public class PlayMusic extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet /* * */ private static final long serialVersionUID = 1L; /* (non-Java-doc) * see javax.servlet.http.HttpServlet#HttpServlet() */ public PlayMusic() super(); /* (non-Java-doc) * see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1