接收postdelphiTIdMultiPartFormDataStreampost中文 (2018-11-09 14:23:08)转载▼
    分类: java
    java
    public org.json.JSONObject baseUpLoadFile(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException, FileUploadException {
    org.json.JSONObject jsonObject = new org.json.JSONObject();
    String filename = “”;
    response.setContentType(“text/html”);
    // 创建文件项目工厂对象
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // 获取系统默认的临时文件保存路径,该路径为Tomcat根目录下的temp文件夹
    String temp = System.getProperty(“;
    // 设置缓冲区大小为 5M
    factory.setSizeThreshold(1024 1024 5);
    // 设置临时文件夹为temp
    factory.setRepository(new File(temp));
    // 用工厂实例化上传组件,ServletFileUpload 用来解析文件上传请求
    ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
    // 解析结果放在List中
    try {
    List< FileItem> list = servletFileUpload.parseRequest(request);
    for (FileItem item : list) {
    String name = item.getFieldName();
    InputStream is = item.getInputStream();
    if (map == null) {
    if (name.contains(“card_type”)) {
    jsonObject.put(“card_type”, inputStream2String(is));
    }
    if (name.contains(“file”)) {
    try {
    if (map == null) {
    filename = DateTime.getNowString(“yyyyMMddHHmmssSSS”) + “.jpg”;
    String temppath = request.getSession().getServletContext().getRealPath((new ImgFilePath()).getProperty(“temp”));
    File fpTemp = new File(temppath);
    // 目录已存在创建文件夹
    if (!fpTemp.exists()) {
    fpTemp.mkdirs();
    }
    inputStream2File(is, temppath + “\” + filename);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    jsonObject.put(“filename”, filename);
    }
    }
    else if (map != null && map.containsKey(“report”)) {
    String reportfilename = “”;
    String subdir = “”;
    if (item.getFieldName().contains(“reportfilename”)) {
    jsonObject.put(“reportfilename”, inputStream2String(item.getInputStream()));
    }
    if (item.getFieldName().contains(“subdir”)) {
    jsonObject.put(“subdir”, inputStream2String(item.getInputStream()));
    }
    if (name.contains(“zfile”)) {
    try {
    if (!jsonObject.get(“reportfilename”).equals(“”)) {
    if (!jsonObject.get(“subdir”).equals(“”)) {
    inputStream2File(is, request.getSession().getServletContext().getRealPath(“/toyorReport”) + “\” + jsonObject.get(“subdir”) + “\” + jsonObject.get(“reportfilename”).toString());
    } else {
    inputStream2File(is, request.getSession().getServletContext().getRealPath(“/toyorReport”) + “\” + jsonObject.get(“reportfilename”).toString());
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    return jsonObject;
    } catch (FileUploadException e) {
    e.printStackTrace();
    return null;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }
    // 流转化成字符串
    public static String inputStream2String(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;
    while ((i = is.read()) != -1) {
    baos.write(i);
    }
    //return baos.toString(“utf-8”);
    return URLDecoder.decode(baos.toString(), “UTF-8”);//===================================================================
    }
    // 流转化成文件
    public static void inputStream2File(InputStream is, String savePath)
    throws Exception {
    System.out.println(“文件保存路径为:” + savePath);
    File file = new File(savePath);
    InputStream inputSteam = is;
    BufferedInputStream fis = new BufferedInputStream(inputSteam);
    FileOutputStream fos = new FileOutputStream(file);
    int f;
    while ((f = fis.read()) != -1) {
    fos.write(f);
    }
    fos.flush();
    fos.close();
    fis.close();
    inputSteam.close();
    }
    //delphi indy10.5.8
    function TfrmRptFilesUpLoad.UploadFile(FileName:String;DesSubDir: String;DesFileName:WideString): Boolean;
    var
    Source: TIdMultiPartFormDataStream ;
    begin
    if FileExists(FileName) then
    begin
    Source := TIdMultiPartFormDataStream.Create;
    Source.AddFormField(‘reportfilename’, HTTPEncode(AnsiToUtf8(DesFileName))); //==================================
    Source.AddFormField(‘subdir’,DesSubDir);
    Source.AddFile(‘zfile’,FileName,’multipart/form-data’);
    idHttpPost.ProtocolVersion := pv1_1 ;
    try
    //qzf 2018-11-9 09:33:48 idHttpPost.Post(StringReplace(FormatURL,’toyorReport’,’User’,[])+’uploadreport.do?report=1’,Source);
    idHttpPost.Post(frmZReport.ServerUrl + ‘User/uploadreport.do?report=1’,Source);
    Source.Free;
    Result:=True;
    except
    Source.Free;
    Result:=False;
    end;
    end
    else
    begin
    Result:=False;
    end;
    end;