您的位置:首页 > 运维架构 > Apache

Apache Cordova 安卓android上传图片或者文件 调用后台服务实现java代码 ft.upload(imageURI, uri, that.uploadSuccess, that.

2017-03-18 21:13 986 查看
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = Number(new Date()) + ".jpg";
options.mimeType = "image/jpeg";
options.headers = {SID:sid,MID:"t1000"};
//服务器路径
var uri = Config.Api;
alert(uri);
var ft = new FileTransfer();
ft.onprogress = function (progressEvent) { };
//执行上传
ft.upload(imageURI, uri, that.uploadSuccess, that.uploadFail, options);

安卓js是通过这种方式,上传图片的是Apache Cordova 控件,后台java代码 如何实现网上介绍的很少。调用的是后台服务,Spring mvc 实现.直接上代码。

import org.apache.commons.fileupload.FileItemIterator;

import org.apache.commons.fileupload.FileItemStream;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.fileupload.util.Streams;

所用到的jar包和引用注意别错了。jar包没有自己下载。看清楚不要引错。

public void upload(HttpServletRequest request, ActionMessage actionMessage) throws Exception {
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {

FileItemStream item = iter.next();
String fileName = item.getName();
InputStream stream = item.openStream();
//方法一  上传到本地
BufferedInputStream in = new BufferedInputStream(stream);//
// 获得文件输入流

BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(new File("D:" + "\\" + fileName)));// 获得文件输出流
Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
方法二。。上传到图服务器得到二进制文件          //上传到图片服务器,已二进制文件上传。
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4099];
int len = -1;
while ((len = stream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] buff=outStream.toByteArray();//二进制文件都得到了,直接调用你们自己方法上传到图片服务器。

}
} catch (Exception e) {

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐