安卓多张图片上传,一张一张的传。这个怎么实现啊。。菜鸟一个。给个思路,

kylodw 2016-09-19 02:31:05
我试着用了asyntask,但是不符合要求啊。。。还有没有其他的。。。
...全文
850 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
_周星星 2016-12-09
  • 打赏
  • 举报
回复
AsyncHttpClient 我们一般用这个框架 http哈 RequestParams直接封装一个File就行
qq_34025020 2016-12-08
  • 打赏
  • 举报
回复
引用 11 楼 line1213 的回复:
[quote=引用 10 楼 qq_34025020 的回复:] [quote=引用 9 楼 line1213 的回复:] 用第三方框架xutils就好了啊,我目前的项目就是用的xutils
你用Xutils是怎么上传的?我这上传只能传上最后一张图片[/quote] 传的图片路径啊,网上很多例子[/quote] 我这是用的图片路径,循环遍历之后只上传最后一张图片
line1213 2016-12-08
  • 打赏
  • 举报
回复
引用 10 楼 qq_34025020 的回复:
[quote=引用 9 楼 line1213 的回复:] 用第三方框架xutils就好了啊,我目前的项目就是用的xutils
你用Xutils是怎么上传的?我这上传只能传上最后一张图片[/quote] 传的图片路径啊,网上很多例子
qq_34025020 2016-12-08
  • 打赏
  • 举报
回复
引用 9 楼 line1213 的回复:
用第三方框架xutils就好了啊,我目前的项目就是用的xutils
你用Xutils是怎么上传的?我这上传只能传上最后一张图片
line1213 2016-09-22
  • 打赏
  • 举报
回复
用第三方框架xutils就好了啊,我目前的项目就是用的xutils
头发还没秃a 2016-09-22
  • 打赏
  • 举报
回复
引用 7 楼 u011330026 的回复:
引用 6 楼 zhumj_zhumj 的回复:
我做了一次性上传多张到服务器的,嘎嘎
咋做的
根据网上的例子做的: UploadThread.java:

public class UploadThread extends Thread {
    private static AdLog logger = AdLog.clog();
    /*上传路径*/
    private static String url="";
    /*参数*/
    private Map<String, String> params = new HashMap<>();
    /*需要上传的文件的列表*/
    private Map<String, File> files = new HashMap<>();
    private static final int TIME_OUT = 15*1000; //超时时间
    private static final String CHARSET = "UTF-8"; //设置编码
    private static final String BOUNDARY = java.util.UUID.randomUUID().toString();
    private static final String PREFIX = "--";
    private static final String LINEND = "\r\n";
    private static final String MULTIPART_FROM_DATA = "multipart/form-data";

    private static FileUploadListener listener;
    public void OnUploadListener(FileUploadListener listener) {
        this.listener = listener;
    }
    public interface FileUploadListener{
        void UploadStart();
        void UploadSucceed(String succeed);
        void UploadFail();
    }

    public UploadThread(String url, Map<String, String> params, Map<String, File> files, FileUploadListener listener){
        this.url = url;
        this.files = files;
        this.params = params;
        this.listener = listener;
    }

    @Override
    public void run() {
        if (listener!=null) {
            listener.UploadStart();
        }
        try {
            URL uri = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
//            conn.setReadTimeout(10 * 1000); // 缓存的最长时间
            conn.setConnectTimeout(TIME_OUT);
            conn.setDoInput(true);// 允许输入
            conn.setDoOutput(true);// 允许输出
            conn.setUseCaches(false); // 不允许使用缓存
            conn.setRequestMethod("POST");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
            // 首先组拼文本类型的参数
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                sb.append(PREFIX);
                sb.append(BOUNDARY);
                sb.append(LINEND);
                sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
                sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
                sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
                sb.append(LINEND);
                sb.append(entry.getValue());
                sb.append(LINEND);
            }
            DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
            outStream.write(sb.toString().getBytes());
            // 发送文件数据
            if (files != null)
                for (Map.Entry<String, File> file : files.entrySet()) {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.append(PREFIX);
                    sb1.append(BOUNDARY);
                    sb1.append(LINEND);
                    sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
                            + file.getValue().getName() + "\"" + LINEND);
//                    sb1.append("Content-Disposition: form-data; name=\"" + "file" + "\"" + LINEND);
                    sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
                    sb1.append(LINEND);
                    outStream.write(sb1.toString().getBytes());
                    InputStream is = new FileInputStream(file.getValue());
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = is.read(buffer)) != -1) {
                        outStream.write(buffer, 0, len);
                    }
                    is.close();
                    outStream.write(LINEND.getBytes());
                }
            // 请求结束标志
            byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
            outStream.write(end_data);
            outStream.flush();
            // 得到响应码
            int res = conn.getResponseCode();
            InputStream in = conn.getInputStream();
            StringBuilder sb2 = new StringBuilder();
            if (res == 200) {
                int ch;
                while ((ch = in.read()) != -1) {
                    sb2.append((char) ch);
                }
            }
            String succeedStr = new String(sb2.toString().getBytes("iso8859-1"), "UTF-8");
            logger.i("UploadThread : succeedStr = " + succeedStr);
            Map<String, Object> returnMap = Converter.string2Map(succeedStr);
            if (Converter.object2Integer(returnMap.get("code")) == 1){
                if (listener != null) {
                    listener.UploadSucceed(Converter.map2String(returnMap));
                }
            }else {
                if (listener != null) {
                    listener.UploadFail();
                }
            }
            outStream.close();
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            if (listener!=null) {
                listener.UploadFail();
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (listener!=null) {
                listener.UploadFail();
            }
        }
    }

}
使用:

        Map<String, String> params = new HashMap<>();//参数map
        params.put("_userId", getPrefHelper().read("_userId").toString());
        params.put("_token", getPrefHelper().read("_token").toString());
        Map<String, File> files = new HashMap<>();//图片map
        File file = new File("图片路径");
        files.put("file", file);
        new UploadThread(ApiClient.getUploadPersonHeadportraitURL(), params, files, new UploadThread.FileUploadListener() {
            @Override
            public void UploadStart() {
                logger.i("UpLoad : 开始");
                mHandler.sendMessage(mHandler.obtainMessage(UPLOAD_START));
            }
            @Override
            public void UploadSucceed(String succeed) {
                logger.i("UpLoad : 成功 = " + succeed);
                mHandler.sendMessage(mHandler.obtainMessage(UPLOAD_SUCCEED));
            }
            @Override
            public void UploadFail() {
                logger.i("UpLoad : 失败");
                mHandler.sendMessage(mHandler.obtainMessage(UPLOAD_FAIL));
            }
        }).start();
还需要后台代码配合:

        DiskFileItemFactory factory = new DiskFileItemFactory();  
        ServletFileUpload upload = new ServletFileUpload(factory);  
        try  
        {  
            List items = upload.parseRequest(request);  
            Iterator itr = items.iterator();  
            while (itr.hasNext())  
            {  
                FileItem item = (FileItem) itr.next();  
                if (item.isFormField())  
                {  
                    System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8"));  
                }   
                else  
                {  
                    if (item.getName() != null && !item.getName().equals(""))  
                    {  
                        System.out.println("上传文件的大小:" + item.getSize());  
                        System.out.println("上传文件的类型:" + item.getContentType());  
                        // item.getName()返回上传文件在客户端的完整路径名称  
                        System.out.println("上传文件的名称:" + item.getName());  
  
                        File tempFile = new File(item.getName());  
                        // 上传文件的保存路径  
                        File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());  
                        item.write(file);  
                        request.setAttribute("upload.message", "上传文件成功!");  
                    } else  
                    {  
                        request.setAttribute("upload.message", "没有选择上传文件!");  
                    }  
                }  
            }  
        }   
        catch (FileUploadException e)  
        {  
            e.printStackTrace();  
        }   
        catch (Exception e)  
        {  
            e.printStackTrace();  
            request.setAttribute("upload.message", "上传文件失败!");  
        }  
        request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);  
shinobu59 2016-09-22
  • 打赏
  • 举报
回复
引用 6 楼 zhumj_zhumj 的回复:
我做了一次性上传多张到服务器的,嘎嘎
咋做的
風言楓語 2016-09-21
  • 打赏
  • 举报
回复
楼上正解 主要是业务逻辑
nodream521 2016-09-21
  • 打赏
  • 举报
回复
跟用什么网络框架没关系,主要是逻辑业务, 比如:未上传图片路径 unUploadList, 已上传图片路径集合 uploadList 上传成功一个后,成功回调里,从unuploadlist中拿出,放入 已上传图片路径集合,再上传第二张,已此类推
头发还没秃a 2016-09-21
  • 打赏
  • 举报
回复
我做了一次性上传多张到服务器的,嘎嘎
无奈的程序猿 2016-09-19
  • 打赏
  • 举报
回复
其实对于安卓来说,用腾讯云处理图像上传和识别还是比较靠谱的,可以试试。
kylodw 2016-09-19
  • 打赏
  • 举报
回复
你好,,你那边有类似的demo么。。让我参考参考
LoveWyf_ 2016-09-19
  • 打赏
  • 举报
回复
可以使用一些第三方封装好的框架,okhttp、volley等

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧