多图片上传Android,服务器端实现

最近时间在做的项目中需要用到Android前端上传服务器,第一次做文件上传的服务器端,查了各种资料,踩了各种坑,记录一下最终的实现方式
**一.服务器端**
    服务器端使用的是ssh框架,文件实现逻辑:
        1.获取用户上传的文件
        2.将文件保存到文件夹
        3.保存成功后将文件的索引存入数据库,
    1.获取用户上传的文件
        完整action如下:
    
public class UploadAction extends ActionSupport implements ModelDriven, ServletResponseAware, ServletRequestAware {
    private HttpServletResponse response;
    private HttpServletRequest request;
    //*********重点******
    // 多文件上传的文件key,相当于获取 request.getParameter(key)
    //如果传入的key不对,上传的文件为空,报空指针异常,
    private List file;

    private List fileFileName;

    private List fileContentType;

    private List dataUrl;

    public void setDataUrl(List dataUrl) {
        this.dataUrl = dataUrl;
    }

    public List getFile() {
        return file;
    }

    public void setFile(List file) {
        this.file = file;
    }

    public List getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(List fileFileName) {
        this.fileFileName = fileFileName;
    }

    public List getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(List fileContentType) {

        this.fileContentType = fileContentType;
    }

    private QualityBean bean = new QualityBean();

    public void setBean(QualityBean bean) {
        this.bean = bean;
    }

    public String upload() throws Exception {
        dataUrl = new ArrayList();
        String imgpath = "D:/loadtest";
        System.out.println("file-size" + file.size());
        if (file != null) {
            for (int i = 0; i < file.size(); ++i) {
                InputStream is = new FileInputStream(file.get(i));
                dataUrl.add(imgpath + this.getFileFileName().get(i));
                File destFile = new File(imgpath, this.getFileFileName().get(i));
                OutputStream os = new FileOutputStream(destFile);
                byte[] buffer = new byte[400];
                int length = 0;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
                is.close();
                os.close();
                //往数据库添加--略
                //返回数据,真实项目中返回自己逻辑判断
                BooleanBean bean = new BooleanBean();
                bean.setUpdate(true);
                DataUtils.writeBeanDate(response, bean);
              
            }
        }
        return null;
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    @Override
    public QualityBean getModel() {
        return bean;
    }
}

structs.xml配置如下






    
    
    
    
        
            upload
        
        
            
            
                
                104857600
                
            
            
            
        
    

如果报错如下,则是上传文件超过设置的大小,

多图片上传Android,服务器端实现_第1张图片

**二.Android 端代码实现**

        final List list = new ArrayList<>();
        list.add("/storage/emulated/0/Boohee/1565153408424.jpg");
        list.add("/storage/emulated/0/Boohee/1564797245581.jpg");
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map params = new HashMap<>();
                RequestBody pictureNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), "file");
                for (String str : list) {
                    File picture = new File(str);
                    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), picture);
                    params.put("file\"; filename=\"" + picture.getName() + "", requestFile);
                }
                mvpPresenter.submit("韶关1店",pictureNameBody, params);
            }
        });

多图片上传Android,服务器端实现_第2张图片

多图片上传Android,服务器端实现_第3张图片

上面file作为文件上传的key,两端必须保持一致

你可能感兴趣的:(retrofit,spring,java,android)