⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http post.txt

📁 java采用http的post方式实现上传下载xml文件
💻 TXT
字号:
1.java采用http的post方式实现上传下载xml文件


以post方式向服务器上的一个cgi(用c语言写的)发送一个文件,
cgi在接受到文件并将文件处理后发回给客户端。现在是我发送一个小文件,
服务器端可以收到

import java.net.*;
import java.io.*;
 
public class HTTP_post
{
    static URL u;
    public static void main(String args[])
    {
        try
        {
            HTTP_post post = new HTTP_post();
            post.u = new URL("http://10.4.0.149/hello/jvs.cgi");
 
            // Open the connection and prepare to POST
            URLConnection uc = u.openConnection();
            uc.setDoOutput(true);
            uc.setDoInput(true);
            uc.setAllowUserInteraction(false);
 
            //uc.addRequestProperty("Content-Type", "text/xml");
            //uc.addRequestProperty("Accept", "*/*");
 
            DataOutputStream dstream = new DataOutputStream(uc.getOutputStream());
 
            // The POST line
 
            String uploadFileName = "e:\\links.xml";
 
            FileInputStream fis1 = null;
 
            File file1 = new File(uploadFileName);
            fis1 = new FileInputStream(file1);
 
            byte[] fileStuff = new byte[512];
            int howMany = -1;
            int totMany = 0;
            howMany = fis1.read(fileStuff, 0, 512);
            while (howMany != -1)
            {
              totMany += howMany;
              dstream.write(fileStuff, 0, howMany);
              System.out.println(new String(fileStuff));
              howMany = fis1.read(fileStuff, 0, 512);
            }
            System.err.println("read " + totMany +
                               " bytes from file, wrote to outputstream.");
            fis1.close();
 
            dstream.close();
 
            // Read Response
            InputStream in = uc.getInputStream();
 
            BufferedReader r = new BufferedReader(new InputStreamReader(in));
            StringBuffer buf = new StringBuffer();
            String line;
            while ((line = r.readLine())!=null) {
                buf.append(line);
                System.out.println(line);
            }
            in.close();
            r.close();
 
        }
        catch (IOException e)
        {
            e.printStackTrace();  // should do real exception handling
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -