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

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

    private List<String> fileFileName;

    private List<String> fileContentType;

    private List<String> dataUrl;

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

    public List<File> getFile() {
        return file;
    }

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

    public List<String> getFileFileName() {
        return fileFileName;
    }

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

    public List<String> getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(List<String> 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>();
        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配置如下

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!-- 上传过程中临时文件存放目录 -->
    <constant name="struts.multipart.saveDir" value="D:/uploadtemp"></constant>
    <constant name="struts.multipart.maxSize" value="104857600"/>
    <package name="UploadDemo" namespace="/" extends="struts-default">
        <global-allowed-methods>
            upload
        </global-allowed-methods>
        <action name="upload_*" class="upload" method="{1}">
            <!-- 定义文件上传拦截器 -->
            <interceptor-ref name="fileUpload">
                <!-- 设置文件上传大小 -->
                <param name="maximumSize">104857600</param>
                <!-- 设置文件上传类型
26                 <param name="allowedTypes">
27                     application/vnd.ms-powerpoint
28                 </param>
29                 -->
            </interceptor-ref>
            <!-- 自定义了拦截器后必手动定义默认的拦截器,否则默认的拦截器不会被执行 -->
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>
    </package>
</struts>

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

clipboard.png

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

        final List<String> 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<String, RequestBody> 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);
            }
        });

clipboard.png

clipboard.png

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

阅读 2.8k
26 声望
7 粉丝
0 条评论
26 声望
7 粉丝
文章目录
宣传栏