怎么发送带有文字和文件的post请求,并且返回文字和文件-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

怎么发送带有文字和文件的post请求,并且返回文字和文件

rt,本人在服务器上写了一个servlet来接收post请求,用网页的form表单测试了一下可以同时接收文本框的内容和文件的上传,但是我想用java程序来发送这个post请求,查了很多,要么只有文本或者只有文件传输,没有同时传送文本和文件的,还有我想服务器返回给客户端的里面也同时包含文本或者文件,怎么做

展开
收起
蛮大人123 2016-03-24 14:27:50 2664 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    服务端:

    public partial class _Default : System.Web.UI.Page
    {
    
        private string id = "";
        private string userName = "";
        private string type = "";
        private string fileName = "";
        //文件长度
        private long contentLength = 0;
        private static readonly string filePath = ConfigurationManager.AppSettings["filePath"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                id = Request["id"];
                userName = Request["user"];
                type = Request["type"];
                fileName = Request.Headers["FileName"];
                writeFile();
            }
        }
    
        /// <summary>
        /// 上传文件
        /// </summary>
        private void writeFile()
        {
            try
            {
                Stream stream = Request.InputStream;
                contentLength = stream.Length;
                string currentFilePath = filePath + userName;
                if (!Directory.Exists(currentFilePath))
                {
                    Directory.CreateDirectory(currentFilePath);
                }
    
                FileStream fileStream = File.Create(currentFilePath + @"\" + fileName);
                //每次读取的1024个字节
                byte[] bytes = new byte[1024];
                
                int numReadByte = 0;
                while ((numReadByte = stream.Read(bytes, 0, 1024)) != 0)
                {
                    fileStream.Write(bytes, 0,numReadByte);
                }
                //关闭流
                stream.Close();
                fileStream.Close();
    
        }

    Java文件上传客户端示例:

    /**
     *  
     * 只是写的一个示例,filePath,和FileName根据需要进行调整。
     */
    public class MyTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            String str="http://localhost:2906/Default.aspx?id=1&user=2&type=3";
            String filePath="D:\\Wildlife.wmv";
            String fileName="Wildlife.wmv";
            try {
                URL url=new URL(str);
                HttpURLConnection connection=(HttpURLConnection)url.openConnection();
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.addRequestProperty("FileName", fileName);
                connection.setRequestProperty("content-type", "text/html");
                BufferedOutputStream  out=new BufferedOutputStream(connection.getOutputStream());
                
                //读取文件上传到服务器
                File file=new File(filePath);
                FileInputStream fileInputStream=new FileInputStream(file);
                byte[]bytes=new byte[1024];
           int numReadByte=0;
                while((numReadByte=fileInputStream.read(bytes,0,1024))>0)
                {
                    out.write(bytes, 0, numReadByte);
                }
    
                out.flush();
                fileInputStream.close();
                //读取URLConnection的响应
                DataInputStream in=new DataInputStream(connection.getInputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    }
    2019-07-17 19:12:44
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载