以下是上传文件的servlet:
package com.newyulong.dw2th.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.newyulong.dw2th.util.file.*;
import javax.naming.NamingException;
import com.newyulong.dw2th.util.db.DataSources;
public class UploadFileServlet extends HttpServlet
{
private static final String PATHSTRING="D://unicom//upload//";
private static final String URLSTRING="unicom/upload/";
private String UPLOADPATH="D://unicom//upload//";
public void init(){
try{
//UPLOADPATH = (String)DataSources.getInstance().getServletPath();
System.out.println("the upload path is:"+UPLOADPATH);
}catch(Exception ax){
System.out.println(ax.getMessage());
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
//这里设定允许上传的文件大小
try{
ContentFactory holder=ContentFactory.getContentFactory(request,(1024*1024*2));
if (holder==null)
{
//可能不是 multipart/form-data 形式编码,或浏览器不支持
out.println("请确认页面中表单的编码为 multipart/form-data 形式");
out.println("如确认,可能你的浏览器不支持该种编码方式");
doGet(request,response);
return;
}
Enumeration fields=holder.getFileParameterNames();
Enumeration names=holder.getParameterNames();
out.println("<html><body>/n<h2 align=center>你刚才上传的文件是:</h2>");
out.println("<table border=1 align=center width=60%>/n<tr>/n");
out.println("<td width=50%>文件名</td>/n<td width=50%>操作</td>/n</tr>");
while(fields.hasMoreElements())
{
String field=(String)fields.nextElement();
FileHolder[] file=holder.getFileParameterValues(field);
for (int i=0;i<file.length ;i++ )
{
String fileName=file[i].getFileName();
File f=new File(UPLOADPATH+fileName);
file[i].saveTo(f);
out.println("<form name=/"upload/" method=/"post/" action=/"SaveDatabaseServlet/">/n");
out.println("<input type=/"hidden/" name=/"filename/" value=/""+UPLOADPATH+fileName+"/">");
out.println("<tr><td><a href=/""+URLSTRING+fileName+"/">"+fileName+"</td>/n");
out.println("<td><input type=/"submit/" value=/"保存到数据库/" name=/"save/"></td>/n</tr></form>");
}
}
/*
out.println("</table>");
out.println("<hr>");
out.println("<h2 align=center>Other Fields</h2>");
out.println("<table border=1 align=center width=90%>/n <tr>/n <td width=40%>字段名</td>/n <td width=60%>Value</td>/n </tr>");
while(names.hasMoreElements())
{
String n=(String)names.nextElement();
String[] values=holder.getParameterValues(n);
for(int i=0;i<values.length;i++)
{
out.println(" <tr>/n <td>" + n+"</td>/n <td>"+values[i]+"</td>/n <tr>");
}
}
*/
out.println("</table>");
out.println("</body></html>");
}catch(ContentFactoryException e)
{
out.println("上载的数据太多");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<form method='post' action='/unicom/UploadServlet' enctype='multipart/form-data'>");
out.println("<p> name: <input type='text' name='name'>");
out.println("<br> introduce:<br>");
out.println("<textarea name='introduce' cols='65' rows='10'></textarea>");
out.println("</p>");
out.println(" <p> ");
out.println(" <input type='checkbox' name='youlike' value='swim'> swim ");
out.println(" <input type='checkbox' name='youlike' value='jump'> jump ");
out.println(" <input type='checkbox' name='youlike' value='climb'> climb ");
out.println(" <input type='checkbox' name='youlike' value='读书'> 读书 ");
out.println(" <input type='checkbox' name='youlike' value='写字'> 写字 <br>");
out.println(" <br> file1 <input type='file' name='file1'> ");
out.println(" <br> file2 <input type='file' name='file2'>");
out.println(" <br> file3 <input type='file' name='file3'>");
out.println(" <br> file4 <input type='file' name='file4'>");
out.println(" <br> file7 <input type='file' name='file7'>");
out.println(" <br> <br>");
out.println(" 数据总量限于2M以内<br>");
out.println(" <input type='submit' name='Submit' value='Submit'>");
out.println(" <input type='reset' value='Reset'>");
out.println(" </p>");
out.println(" </form>");
out.println("</html>");
}
}
以下是将数据保存到数据库的servlet:
package com.newyulong.dw2th.servlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.StringTokenizer;
import java.util.ArrayList;
import com.newyulong.dw2th.util.file.*;
import javax.naming.NamingException;
import com.newyulong.dw2th.util.db.DataSources;
import com.newyulong.dw2th.util.comm.ApplicationException;
public class SaveDatabaseServlet extends HttpServlet {
private transient Connection dbConnection = null;
private transient DataSource datasource = null;
public void init() {
}
public DataSource getDataSource() throws NamingException,ApplicationException {
try {
datasource = (DataSource)DataSources.getInstance().getDataSource();
} catch (NamingException ne) {
System.out.println(ne.getMessage());
}
return datasource;
}
private Connection getDBConnection() throws ApplicationException
{
try {
dbConnection = datasource.getConnection();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
return dbConnection;
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String fileName = request.getParameter("filename");
FileReader fileReader = new FileReader(fileName);
BufferedReader br = new BufferedReader(fileReader);
boolean eof = false;
String sql ="";
while (!eof)
{
String line = br.readLine();
if (line==null)
{
eof = true;
}
else
{
StringTokenizer st = new StringTokenizer(line);
ArrayList list = new ArrayList();
PreparedStatement stmt = null;
while (st.hasMoreTokens())
{
list.add(st.nextToken());
}
sql = " insert into latency_customer values ('"+(String)list.get(0)+
"','"+(String)list.get(1)+"')";
try{
getDataSource();
dbConnection=getDBConnection();
stmt=dbConnection.prepareStatement(sql);
stmt.executeUpdate();
}catch(NamingException ne){
System.out.print(ne.getMessage());
}catch(ApplicationException ex){
System.out.print(ex.getMessage());
}catch(SQLException sx){
System.out.println(sx.getMessage());
}finally{
try
{
stmt.close();
dbConnection.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}
}
}