今天要做一个图片异步上传的功能,需求是这样的
在官网让玩家提出问题反馈,只是再发聩是需要玩家上传图片作为客服审核的证据。为了保证官网的安全性,于是准备了一台图片服务器,专门存储图片。
我的思路是在页面上传文件到图片服务器,并返回图片的地址。然后再提交表单时,将图片url,保存入数据库。
1. 利用 ajaxfileupload进行上传时,是不能进行跨域操作的。
2. 所以更换为jquery.fileupload进行跨域操作,不过没有中文文档,使用时很是费劲。因为第一次上传没有反应,只有第二次的时候才会成功,让人很头疼。我对js又不是很熟,所以准备暂且放置一下。用我比较熟的服务器端的开发来完成。
3. 在本本服上传文件和信息。然后利用httpclient 向图片服务器发送请求进行保存。这个流程比较好控制。
下面是httpclient的代码
public static void main(String[] args) throws ClientProtocolException,
IOException {
HttpClient httpclient = new DefaultHttpClient();
// 请求处理页面
HttpPost httppost = new HttpPost(
"http://localhost:8080/LogTest/test/upload");
// 创建待处理的文件
FileBody file = new FileBody(new File(
"E:\\worktools\\resin-pro-3.1.13\\conf\\resin.conf"));
// 创建待处理的表单域内容文本
StringBody descript = new StringBody("0431.la");
// 对请求的表单域进行填充
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("fileName", file);
reqEntity.addPart("descript", descript);
// 设置请求
httppost.setEntity(reqEntity);
// 执行
HttpResponse response = httpclient.execute(httppost);
// HttpEntity resEntity = response.getEntity();
// System.out.println(response.getStatusLine());
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
// 显示内容
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
if (entity != null) {
entity.consumeContent();
}
}
}
明天会将jquery.fileupload的代码优化以后,再将该js代码上传。