记一次 uniapp上传图片 java后台接收 由于赶时间做 所以没有细研究 但对于我uniapp小白来说 也是费了好大劲才copy到位

编程入门 行业动态 更新时间:2024-10-07 20:34:31

记一次 uniapp上传图片  java后台接收  由于赶时间做 所以没有细研究 但对于我uniapp小白来说 也是费了<a href=https://www.elefans.com/category/jswz/34/1733587.html style=好大劲才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到位

本文发布于:2024-02-06 12:32:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1749003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:好大   上传图片   后台   时间   uniapp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!