好大劲才copy到位"/>
记一次 uniapp上传图片 java后台接收 由于赶时间做 所以没有细研究 但对于我uniapp小白来说 也是费了好大劲才copy到位
uniapp的 上传不知道你们觉得怎么样, 但是对我小白来说 挺恶心 首先 uni.uploadFile({})方法不能穿文件类型回服务器
而且加上header{'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8',
'Connection':'keep-alive',
'Content-Type':'multipart/form-data',
'Content-Type':'application/json,charset=utf-8',
'source-business':'EPM'} 无论怎么改也不行。 最好选择放弃 uni.uploadFile 拜拜了您内~~~~
然后把目光放在 uni.request() 调了小半天 终于出效果了。。。
uniapp.....
<template>
<view>
<button type="primary" @click="upload">选择照片</button>
</view>
</template>
<script>
export default {
methods: {
upload: function() {
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: function(res) {
const tempFilePaths = res.tempFilePaths;
uni.request({
url: tempFilePaths[0],
method: 'GET',
responseType: 'arraybuffer',
success: ress => {
let base64 = wx.arrayBufferToBase64(ress.data); //把arraybuffer转成base64
base64 = 'data:image/jpeg;base64,' + base64 //不加上这串字符,在页面无法显示的哦
uni.request({
url: 'http://localhost/api/v2/file/upload',
dataType: 'file',
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: {
'da': base64
},
success: function(uploadFileRes) {
console.log(uploadFileRes.data);
}
});
}
})
}
});
}
}
}
</script>
java 后台接收 用的是jfinal框架
@ActionKey("/api/v2/file/upload") public void UploadFile() {String savePath = new SimpleDateFormat("yyyyMMdd").format(new Date())+"/";String strImg = this.getPara("da");strImg = strImg.substring(strImg.lastIndexOf("base64,")+7,strImg.length());String location = PathKit.getWebRootPath() + "/upload/phone/"+savePath;File file = new File(location);if(!file.exists()){file.mkdirs();}String fileName = System.currentTimeMillis()+".jpg";try {GenerateImage(strImg,location+fileName);} catch (IOException e) {e.printStackTrace();this.renderJson(new Record().set("code",500));}this.renderJson(new Record().set("code",200).set("msg",location+fileName)); }
public static boolean GenerateImage(String imgStr, String imgFilePath) throws IOException {// 对字节数组字符串进行Base64解码并生成图片if (imgStr == null) // 图像数据为空return false;BASE64Decoder decoder = new BASE64Decoder();// Base64解码byte[] bytes = decoder.decodeBuffer(imgStr);for (int i = 0; i < bytes.length; ++i) {if (bytes[i] < 0) {// 调整异常数据bytes[i] += 256;}}// 生成jpeg图片OutputStream out = new FileOutputStream(imgFilePath);out.write(bytes);out.flush();out.close();return true;}
OK 完事儿。。 直接将图片的Base64码 保存在本地了
更多推荐
记一次 uniapp上传图片 java后台接收 由于赶时间做 所以没有细研究 但对于我uniapp小白来说 也是费了好大劲才copy到位
发布评论