java download

来源:互联网 发布:libevent源码解析 编辑:程序博客网 时间:2024/06/05 00:09

 public String exportTemplete(final TempleteParamDto dto,

                            final List<XlsTempleteConfig> templeteConfig) throws IOException {

                   final String excelFileType = "xls";

                   final String fileName = dto.getFileName();

                   final Workbook wb;

                   final String filePath = dto.getFilePath();

                   if (StringUtil.isNotEmptyString(filePath)) {

                            wb = new HSSFWorkbook(ProcessorFactory.class.getClassLoader().getResourceAsStream(dto.getFilePath()));                            

                   }else{

                            wb = ImportXlsFactory.getWorkbook(excelFileType);

                            writeHeader(wb, templeteConfig);

                   }

                   final String userName = DataBusFactory.getDataBus().getLogonInfo()

                                     .getUserName();

                   String fPath;

                   FileOutputStream fout = null;

                   try {

                            final File f = ImportXlsFactory.createTempFile(userName, excelFileType, filePath);

                            fout = new FileOutputStream(f);

                            wb.write(fout);

                            fPath = (new StringBuilder("/aaa/menu/DownLoadServlet.do?uri=")).append(

                                               URLEncoder.encode(f.getPath(), "UTF-8"))

                                               .toString();

                            if (!StringUtil.isEmptyString(fileName)) {

                                     fPath += "&fileName=" + fileName + "." + excelFileType;

                            }

                            return fPath;

                   } catch (final Exception e) {

                            e.printStackTrace();

                            throw new FmisRuntimeException(e);

                   } finally {

                            try {

                                     if (null != fout) {

                                               fout.close();

                                     }

                            } catch (final IOException e) {

                                     e.printStackTrace();

                            }

                   }

         }

 

 

/**
 * TODO 在此写上类的相关说明.<br>
 */

public class DownLoadServlet extends HttpServlet {

         @Override

         protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,

                            IOException {

                   // super.doPost(req, resp);

                   req.setCharacterEncoding("UTF-8");

                   resp.setCharacterEncoding("UTF-8");

                   final String uri = req.getParameter("uri");

                   if (StringUtil.isEmptyString(uri)) {

                            resp.flushBuffer();

                            return;

                   }

                   String fileName = req.getParameter("fileName");

                   if (StringUtil.isEmptyString(fileName)) {

                            int index = uri.lastIndexOf("/");

                            if (index == -1) {

                                     index = uri.lastIndexOf("\\");

                            }

                            fileName = uri.substring(index + 1);

                            fileName = new String(fileName.getBytes(), "ISO-8859-1");

                   }

                   resp.setHeader("Content-Disposition", "attachment; filename=" + fileName);

                   FileInputStream is = null;

                   ServletOutputStream os = null;

                   try {

                            is = new FileInputStream(uri);

                            os = resp.getOutputStream();

                            final byte buffer[] = new byte[1024];

                            int c;

                            while ((c = is.read(buffer)) > 0) {

                                     os.write(buffer, 0, c);

                            }

                            resp.flushBuffer();

                   } catch (final Exception e) {

                            e.printStackTrace();

                   } finally {

                            if (null != is) {

                                     is.close();

                            }

                            if (null != os) {

                                     os.close();

                            }

                   }

 

         }

 

         @Override

         protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,

                            IOException {

                   doPost(req, resp);

         }

}

 

 

loadTemplete : function() {

                            if(outerApi.filePath&&(outerApi.filePath+"").charAt(0)=="/"){

                            x_window.location= outerApi.filePath;

                            return ;

                            }

                            interact.exportTemplete({

                                               p0 : $.toJSON({

                                                                           filePath : outerApi.filePath||"",

                                                                           fileName:outerApi.fileName||""

                                                                 }),

                                               p1 : $.toJSON(outerApi.cms)

                                     }, function(api) {

                                               if (api.error) {

                                                        messageUtils.alert(api.context);

                                                        return;

                                               }

                                               //"/aaa/menu/DownLoadServlet.do?uri=C%3A%5CDocuments+and+Settings%5Cwangli3%5CjqGridExcel%5Cxjx%5C1378948899665.xls&fileName=导入模板.xls"

                                               x_window.location= api.context;

                                     });              

                  

                   },