1、windows下启动tomcat(startup.bat)报错退出dos窗口问题
2、mysql默认值设置为当前时间
3、下载文件到用户自定义的路径,包含火狐下载的中文文件名乱码问题解决
4、java模拟http post请求
5、maven项目直接在lib中加入jar包,pom.xml写法
一、windows下启动tomcat(startup.bat)报错退出dos窗口问题
在windows系统中,把程序发布在tomcat的webapp目录下,启动tomcat(startup.bat)。如果启动过程出错,startup.bat会退出当前窗口,导致无法捕捉错误信息。
解决方法:
1、右键编辑startup.bat,找到 call ”%EXECUTABLE%“ start %CMS_LINE_ARGS% 改为 call ”%EXECUTABLE%“ run %CMS_LINE_ARGS%
2、保存并双击重启startup.bat。
二、mysql默认值设置为当前时间
1、sql语句
CREATE TABLE `test_def_date` (
`id` INT(11) NOT NULL,
`init_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
COLLATE='gbk_chinese_ci'
ENGINE=MyISAM
;
2、heidi可视化工具
默认值一列勾选“CURRENT_TIMESTAMP”。
三、下载文件到用户自定义的路径,包含火狐下载的中文文件名乱码问题解决
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String filename = "2016_04_工资.xls";
if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") != -1) {//判读浏览器类型是不是火狐
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GB2312"),"ISO-8859-1"));
} else {
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
}
//最后将要下载的内容write到response.getOutputStream()这个输出流就可以了。
四、java模拟http post请求
1、请求中只包含普通参数
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
//设置参数
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("userName", userName));//普通参数
postParams.add(new BasicNameValuePair("password", password));//普通参数
post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
//执行请求
HttpResponse response = client.execute(post);
//处理响应
HttpEntity entity = response.getEntity();
String answer = EntityUtils.toString(entity);//接口返回的响应信息
2、模拟文件上传
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus;
PostMethod postMethod = new PostMethod(url);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
postMethod.getParams().setUriCharset("UTF-8");
List<Part> parts = new ArrayList<Part>();
String exampleFileath = this.getClass().getClassLoader().getResource("att/").getPath();
exampleFileath = URLDecoder.decode(exampleFileath, "UTF-8");
parts.add(new FilePart("pic1", new File(exampleFileath + "example1.jpg")));
parts.add(new FilePart("pic2", new File(exampleFileath + "example2.jpg")));
parts.add(new StringPart("attType", attType));
parts.add(new StringPart("channelId", channelId));
parts.add(new StringPart("api_token", api_token));
parts.add(new StringPart("check_sum", check_sum));
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), postMethod.getParams()));
HttpClient client = new HttpClient();
String answer = null;
try {
int resultCode = client.executeMethod(postMethod);
if (resultCode == HttpStatus.SC_OK) {
answer = postMethod.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
postMethod.releaseConnection();
}
五、maven项目直接在lib中加入jar包,pom.xml写法
<dependency>
<groupId>xxx.com.cn</groupId>
<artifactId>ucenter</artifactId>
<version>1.0.0.alpha.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/xxx.jar</systemPath>
</dependency>