Source Code Refrence :
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String filename = (String)request.getParameter("filename");
String original_filename = (String)request.getParameter("original_filename");
ServletOutputStream op = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String realFileNamePath = context.getRealPath(filename);
File f = new File(realFileNamePath);
int length = 0;
response.setContentType("application/octet-stream");
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition",
"attachment; filename=\"" + original_filename + "\"" );
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
}
2010年2月24日 星期三
將中文(UTF-8) 寫入MySQL DB for Java Servlet
1. get request parameter before set Encode
request.setCharacterEncoding("UTF-8");
2. add connection string parameter
useUnicode=true&characterEncoding=UTF-8
Ex.
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/demo?
useUnicode=true&characterEncoding=UTF-8", "userid", "password");
request.setCharacterEncoding("UTF-8");
2. add connection string parameter
useUnicode=true&characterEncoding=UTF-8
Ex.
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/demo?
useUnicode=true&characterEncoding=UTF-8", "userid", "password");
訂閱:
文章 (Atom)