excel文件下载,数据库字段datafiled检索--JS URL传值给servlet乱码

excel文件下载,数据库字段datafiled检索--JS URL传值给servlet乱码

 

1 //javascript里面加密两次,两次才可以的。2 var url = "servlet/getText?name=" + encodeURI(encodeURI(name));
< script  language ="javascript"  type ="text/javascript" >
        function show()
        {
         var name="test";
         var admin="ok";
         var url = "http://localhost:7001/sosuo/ggld/fleet?reloadVessel=" 
           + encodeURI(encodeURI(name))+"
&reloadVoyage ="+ encodeURI(encodeURI(admin));
            window.open(url);
        }
    
</ script >

1 //在java里面,通过指定的编码解密即可。2 String name = URLDecoder.decode(request.getParameter("name"),"utf-8");

web.xml配置:
< servlet >   <!--  新添加 车队 download  -->          < servlet-name > fleetDataDownload </ servlet-name >          < servlet-class > com.cenin.util.FleetDataDownload </ servlet-class >      </ servlet >
< servlet-mapping >          < servlet-name > fleetDataDownload </ servlet-name >          < url-pattern > /ggld/fleet </ url-pattern >      </ servlet-mapping >

DataDownload.java页面:

package com.cenin.util;


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.sql.*;

import org.apache.commons.digester.Digester;
import org.apache.log4j.Logger;
import com.cenin.database.DBManager;



public class DataDownload  extends HttpServlet 
{
    private static   Logger logger = Logger.getLogger(DataDownload .class);
    
    public void init(ServletConfig config) throws ServletException 
    {
        super.init(config);
        try
        {
        }
        catch(Exception ex)
        {
            logger.info(ex.getMessage());
        }

    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException 
    {
        doPost(request,response);
    }
    public void doDownload(HttpServletRequest request, HttpServletResponse response, String fileName, String vessel, String voyage)
    {
        try
        {
            
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "inline; filename=\""+ fileName  + "\"");
            ServletOutputStream sos = response.getOutputStream();
            String title = "数据导出";


            String[] fieldTitles = {"船名", "航次", "提单号", "箱号", "铅封号", "箱型", "货名", "重量", "体积", "发货人", "收货人", "装货港", "卸货港"};
            String[] fieldNames = {"szVessel", "szVoyage", "szBlNo", "szCtnNo", "szSealNo", "szCtnType", "szCargoName", "fWeight", "fVolume", "szReceiver", "szSender", "szLoadPortCode", "szDischargePortCode"};
            int[] widths = {100, 50, 100, 120, 120, 50, 100, 50, 50, 80, 80, 80, 80};

            Connection conn = DBManager.getInstance().getConnection();
            Statement stmt=conn.createStatement(); 
            String sql = "select * from NmhContainer where szVessel='" + 
                    vessel + "' and szVoyage='" + voyage + "'"; // and szBlNo='" + blno + "'";
            ResultSet rs = stmt.executeQuery(sql);
            OutputUtil.excelOutput(title, fieldTitles, fieldNames, widths, rs, sos, "ISO-8859-1", "GBK");
            
            DBManager.getInstance().freeDBResource(rs, stmt, conn);
            
        }
        catch(Exception ex)
        {
            logger.info(ex.getMessage());
        }
        
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        try
        {
            String vessel = request.getParameter("reloadVessel");
            String voyage = request.getParameter("reloadVoyage");
            //String blno = request.getParameter("reloadBlno");
            String name = vessel + "_" + voyage + "_" + ".xls";
            doDownload(request, response, name, vessel, voyage);
            
        }
        catch(Exception ex)
        {
            logger.info(ex.getMessage());
        }

        
        
    }
}

OutputUtil.java页面:

package com.cenin.util;
/*
 * 输出PDF, Excel等格式
 * 2005.5.12 by chenyong@cenin
 * 2005.12.19
 */


import jxl.Workbook;
import jxl.write.*;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Phrase;
import com.lowagie.text.Element;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import org.apache.log4j.Logger;


public class OutputUtil  
{
    private static Logger logger = Logger.getLogger(OutputUtil.class);
    /*******************************************************
    * pdf输出表格
    * title为pdf head
    * fieldtitles为 表头项目名称数组
    * filenames 为  对象属性名数组
    * widths 为 宽度%数组
    * rs为resultset
    * os 为输出流
    * codefrom , codeto 如果需要编码转换
    *******************************************************/    
    public static void excelOutput(String title, String[] fieldtitles, String[] fieldnames, int[] widths, ResultSet rs, OutputStream os, String codefrom, String codeto)
    {
        
        try
        {
            int fieldnumber = fieldnames.length; //, cellnumber = fieldnumber*5;
            if(fieldnumber!=fieldtitles.length||fieldnumber!=fieldnames.length)
                return;

            WritableWorkbook workbook = Workbook.createWorkbook(os);
            WritableSheet sheet = workbook.createSheet(title, 0);
            
            //add field title
            for(int i=0;i
< fieldnumber ;i++)
            {
                //String temp 
= new  String(fieldtitles[i].getBytes(codefrom), codeto);
                sheet.addCell(new Label(i, 0, fieldtitles[i]));
                
            }
            //add values
            int i
=0;
            
while(rs.next())
            {
                for(int j
=0;  j<fieldnames.length; j++)
                {
                    if(rs.getString(fieldnames[j])!
=null)
                    
{
                        //String temp1 
= rs.getString(fieldnames[j]);
                        
String temp  = new  String(rs.getString(fieldnames[j]).getBytes("ISO-8859-1"), "GBK");
                        sheet.addCell(new Label(j, i+1, temp));
                    }
                    else
                        sheet.addCell(new Label(j, i+1, ""));
                }
                i++;
            }
            //write to excel
            workbook.write();
            workbook.close();
            
        }
        catch(Exception e)
        {
            logger.info(e.toString());
        }
            
    }

}

/*例子
FileOutputStream os 
= new  FileOutputStream("e:\\a.xls");
String[] titles 
= {"系统编号","员工","用户帐号"};
String[] fieldnames  = {"lsystemUserId","staff.szname","szaccount"};
int[] widths  = {20,20,20}; 
//OutputUtil.pdfOutput("my pdf输出", titles, fieldnames, widths,objectList,os);
//OutputUtil.excelOutput("excel输出", titles, fieldnames, widths,objectList,os);

*/

 

你可能感兴趣的:(excel文件下载,数据库字段datafiled检索--JS URL传值给servlet乱码)