delphi2种上传方法,.net 用1, java用2,
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, Gauges, IdComponent,
    IdTCPConnection, IdTCPClient, IdHTTP, IdBaseComponent, IdAntiFreezeBase,
    IdAntiFreeze, ImgList,IdMultipartFormData,httpapp;
    方法1:
    function TfrmRptFilesUpLoad.UploadFile(FileName: string; DesSubDir: string; DesFileName: string): Boolean;
    var
    Source: TMemoryStream;
    Response: TStringStream;
    WorkPostContentStream: TStringStream;
    begin
    if FileExists(FileName) then
    begin
    Response := TStringStream.Create(‘’);
    Source := TMemoryStream.Create;
    WorkPostContentStream := TStringStream.Create(‘’);
    Source.LoadFromFile(FileName);
    idHttpPost.ProtocolVersion := pv1_1;
    try
    WorkPostContentStream.CopyFrom(Source, Source.Size);
    idHttpPost.Post(StringReplace(FormatURL, ‘toyorReport’, ‘User’, []) + ‘uploadreport.do?filename=’ + DesFileName + ‘&subdir=’ + DesSubDir, WorkPostContentStream, Response);
    WorkPostContentStream.Free;
    Response.Free;
    Source.Free;
    Result := True;
    except
    WorkPostContentStream.Free;
    Response.Free;
    Source.Free;
    Result := False;
    end;
    end
    else
    begin
    Result := False;
    end;
    end;
    方法2:
    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’,AnsiToUtf8(DesFileName));
    Source.AddFormField(‘subdir’,DesSubDir);
    Source.AddFile(‘zfile’,FileName,’multipart/form-data’);
    idHttpPost.ProtocolVersion := pv1_1 ;
    try
    idHttpPost.Post(StringReplace(FormatURL,’toyorReport’,’User’,[])+’uploadreport.do?report=1’,Source);
    Source.Free;
    Result:=True;
    except
    Source.Free;
    Result:=False;
    end;
    end
    else
    begin
    Result:=False;
    end;
    end;
    下载文件:
    function TfrmRptFilesSynchronization.DownLoadFile(URL: String;Des:WideString;Info:String): Boolean;
    var MyStream:TMemoryStream;
    begin
    DownBytes:=0;
    AllBytes:=0;
    IdAntiFreeze.OnlyWhenIdle:=False;
    MyStream:=TMemoryStream.Create;
    try
    pnlStatus.Caption:=Info;
    pnlStatus.Refresh;
    IdHTTP.Get(URL,MyStream);
    except
    MyStream.Free;
    pnlStatus.Caption:=’文件获取失败,请检查您的地址是否正确,并验证您的网络是否通畅!’ + #10#13 +
    ‘同时请检查您的服务器IIS目录安全性,应设为允许启用匿名访问。’;
    Result:=False;
    Exit;
    end;
    MyStream.SaveToFile(Path+ Utf8ToAnsi(Des));
    MyStream.Free;
    pnlStatus.Caption:=’指定的文件已经成功的获取到相应的目录!’;
    pnlStatus.Refresh;
    Result:=True;
    end;
    .net 后台:
    private void Page_Load(object sender, System.EventArgs e)
    {
    string filename=Request[“filename”];
    string subdir=Request[“subdir”];
    string SaveFileName=””;
    if (subdir!=””)
    {
    SaveFileName=@Server.MapPath(“.”)+@””+subdir+@””+@filename;
    }
    else
    {
    SaveFileName=@Server.MapPath(“.”)+@””+@filename;
    }
    Response.Write(SaveFileName);
    Response.Write(“InputStream.Len=”+Request.InputStream.Length);
    BinaryReader br = new BinaryReader(Request.InputStream);
    Byte[] FileInfo = br.ReadBytes((int)Request.InputStream.Length);
    FileStream fs = new FileStream(SaveFileName, FileMode.Create);
    BinaryWriter w = new BinaryWriter(fs);
    w.Write(FileInfo);
    w.Close();
    fs.Close();
    }
    java后台:
    //打印组件
    @RequestMapping(“/loginreport”)
    public void loginreport(String compno, String username, String password ,HttpServletResponse response) {
    if (username == null || compno == null) {
    outString(“0”, response);
    return;
    }
    User user = userService.login(compno, username, password);
    if (user != null) {
    outString(“1”, response);
    } else {
    outString(“0”, response);
    }
    }
    @RequestMapping(value = “/uploadreport”,method = RequestMethod.POST)
    public void uploadreport(String report,HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException {
    Map map = new HashMap();
    map.put(“report”,report);
    org.json.JSONObject jsonObjectParam = baseUpLoadFile(request, response,map);
    if(jsonObjectParam == null){
    //outErrorInfo(“-1000”,”上传文件出错”,response);
    return;
    }
    }
    //打印组件
    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 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”;
    inputStream2File(is, request.getSession().getServletContext().getRealPath((new ImgFilePath()).getProperty(“temp”)) + “\” + 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”);
    }
    // 流转化成文件
    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();
    }