当前位置: 编程技术>综合
本页文章导读:
▪jquery ajax 与servlet间乱码解决方案 中文乱码问题,解决办法其实很简单,传之前encode,传之后decode。看相关源码片段:
servlet
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(Htt.........
▪String,StringBuffer,StringBuilder的性能比较 首先看多次相加
String 循环10000次,后两者是100000,累加次数是String的10倍
long start = System.currentTimeMillis();
String result = "";
for (int i = 0; i < 10000; i++) {
String s1 = "hello";
Str.........
▪下载文件 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class DownLoadUtil {
private stat.........
[1]jquery ajax 与servlet间乱码解决方案
来源: 互联网 发布时间: 2013-11-10
中文乱码问题,解决办法其实很简单,传之前encode,传之后decode。看相关源码片段:
servlet
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); String userId = request.getParameter("userId"); String vcard = VcardHandle.queryVcard(userId); pw.print(URLEncoder.encode(vcard, "UTF-8")); pw.close(); }
JavaScript
$.ajax({ type : "POST", contentType : "application/x-www-form-urlencoded; charset=utf-8", async : false, url : "VcardServlet?userId=" + userId, dataType : 'text', success : function(result) { vcard = decodeURIComponent(result); } });
需要注意的是:JavaScript中encodeURI 不对下列字符进行编码:“:”、“/”、“;”和“?”,建议使用encodeURIComponent
dml@2013.1.8
作者:duanml61 发表于2013-1-8 17:07:34 原文链接
阅读:4 评论:0 查看评论
[2]String,StringBuffer,StringBuilder的性能比较
来源: 互联网 发布时间: 2013-11-10
首先看多次相加
String 循环10000次,后两者是100000,累加次数是String的10倍
long start = System.currentTimeMillis(); String result = ""; for (int i = 0; i < 10000; i++) { String s1 = "hello"; String s2 = "world"; int s3 = 1000; result += s1; result += s2; result += s3; } System.out.println(System.currentTimeMillis() - start); start = System.currentTimeMillis(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 100000; i++) { String s1 = "hello"; String s2 = "world"; int s3 = 1000; sb.append(s1); sb.append(s2); sb.append(s3); } sb.toString(); System.out.println(System.currentTimeMillis() - start); start = System.currentTimeMillis(); StringBuffer sb2 = new StringBuffer(); for (int i = 0; i < 100000; i++) { String s1 = "hello"; String s2 = "world"; int s3 = 1000; sb2.append(s1); sb2.append(s2); sb2.append(s3); } sb2.toString(); System.out.println(System.currentTimeMillis() - start);
看下运行时间
结果是:7549 36 33比较稳定
6687 34 31
这是就先累加10次(内循环10次累加),但必须要循环执行多次(外循环)才能看出效果!这里循环10W次。,都一样。
long start = System.currentTimeMillis(); String s = "hello world"; for (int i = 0; i < 100000; i++) { String result = ""; for(int j=0; j<10; j++){ result += s; } } System.out.println(System.currentTimeMillis() - start); start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { StringBuilder sb = new StringBuilder(); for(int j=0; j<10; j++){ sb.append(s); } sb.toString(); } System.out.println(System.currentTimeMillis() - start); start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { StringBuffer sb2 = new StringBuffer(); for(int j=0; j<10; j++){ sb2.append(s); } sb2.toString(); } System.out.println(System.currentTimeMillis() - start);
结果为:
304 88 117
如果,把累加次数改为20次(内循环,j<20)
806 164 209
30次, 性能提升还是比较大的。(当然是在循环10W的基础上)
1397 275 483
想起来以来看过的一篇帖子,比较的JSP和PHP运算字符串的能力。
看结果就知道用的是String累加的!这种比较没意思!
作者:gaotong2055 发表于2013-1-8 16:53:13 原文链接
阅读:13 评论:0 查看评论
[3]下载文件
来源: 互联网 发布时间: 2013-11-10
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class DownLoadUtil { private static Logger logger = Logger.getLogger(DownLoadUtil.class); /** * 下载文件 * @param name 用户下载的文件名(*****.***) * @param filePath 文件路径 * @param response * @param fileType 文件类型 * @return * @throws Exception */ public static boolean downLoadFile(String name,String filePath, HttpServletResponse response, String fileType) throws Exception { logger.info("start invoke downLoadFile,[filePath:"+filePath+" , fileType:"+fileType+"]"); File file = new File(filePath); //设置文件类型 if("pdf".equals(fileType)){ response.setContentType("application/pdf"); }else if("xls".equals(fileType)){ response.setContentType("application/msexcel"); }else if("doc".equals(fileType)){ response.setContentType("application/msword"); } response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(name.getBytes("GB2312"), "ISO8859-1") + "\""); //response.setHeader("Content-Disposition", "attachment;filename=\""+ URLEncoder.encode(name, "UTF-8")+ "\""); response.setContentLength((int) file.length()); byte[] buffer = new byte[4096];// 缓冲区 BufferedOutputStream output = null; BufferedInputStream input = null; try { output = new BufferedOutputStream(response.getOutputStream()); input = new BufferedInputStream(new FileInputStream(file)); int n = -1; while ((n = input.read(buffer, 0, 4096)) > -1) { output.write(buffer, 0, n); } output.flush(); response.flushBuffer(); } catch (Exception e) { logger.error("exception when invoke downLoadFile",e); throw e; } finally { if (input != null) input.close(); if (output != null) output.close(); } logger.info("end invoke downLoadFile!"); return true; } }
调用 DownLoadUtil.java
public ActionForward downLoadExcelModel(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { String busType = request.getParameter("busType"); String path = request.getSession().getServletContext().getRealPath( "/resources"); String name = null; String fileName = null; //服务器上文件名 fileName = "xxxl.xls"; name = "xxxx.xls"; } String filePath = path + System.getProperty("file.separator") + fileName; DownLoadUtil.downLoadFile(name,filePath, response, "xls"); return null; }
作者:Heng_Ji 发表于2013-1-8 16:31:03 原文链接
阅读:38 评论:0 查看评论
最新技术文章: