多圖片上傳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,兩端必須保持一致apache

相關文章
相關標籤/搜索