ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java客户端通过Http发送POST请求上传文件到web服务器

2019-08-14 22:39:03  阅读:174  来源: 互联网

标签:web Java string java filePath private connection new Http


原文链接:http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html

1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.net写一个页面负责处理客户端上传的文件,并根据传递的参数把文件保存到相应的目录。客户端是手机应用程序,因为没学过Android,所以我只是写了一个Java的Demo用来上传文件。

服务端:

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文件上传客户端示例,(几年没搞java有点生疏了):

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *  
 * 只是写的一个示例,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(); } } }

转载于:https://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html

标签:web,Java,string,java,filePath,private,connection,new,Http
来源: https://blog.csdn.net/weixin_30325793/article/details/99614396

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有