若干小知识点记录2

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值