分享8:文件操作(java模拟上传下载)

需求:
使用java通过下载连接下载数据并上传到其他服务器。


下载:

第一:下载网络文件:

/**
     * 下载网络文件
     * @throws MalformedURLException
     */
    public void downloadNet(String neturl, String outurl) throws MalformedURLException {
        // 下载网络文件
        int byteread = 0;
        if(neturl == null || "".equals(neturl)) {//测试用例
            neturl = "https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike272%2C5%2C5%2C272%2C90/sign=6f48f892e324b899ca31716a0f6f76f0/54fbb2fb43166d22083ac7064c2309f79152d2d7.jpg";
        }
        if(outurl == null || "".equals(outurl.trim())) {//测试用例
            outurl = "d:/abc.jpg";
        }

        URL url = new URL(neturl);

        try {
            URLConnection conn = url.openConnection();//打开连接
            InputStream inStream = conn.getInputStream();//获取输入流程
            FileOutputStream fs = new FileOutputStream(outurl);//输出流

            byte[] buffer = new byte[1204];//缓冲
            while ((byteread = inStream.read(buffer)) != -1) {//判断是否有数据,并读取
                fs.write(buffer, 0, byteread);//输出
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

第二:通过现有下载连接下载数据

数据抓包结果:我们可以直接在返回的数据中获取文件名称
在这里插入图片描述

    public String Cookie = "JSESSIONID=abcAanHBnnsYu7tMUKl6w;";
	 /**
     * 通过下载连接下载数据
     * @param loadurl
     */
    public String downloadNetLoad(String loadurl, String outurl, String faname) throws IOException {
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(loadurl);
        //设置请求头
        getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
        //设置cookie,如果免登陆下载不需要设置
        getMethod.setRequestHeader("Cookie", Cookie);//设置,原下载不验证的话,无需设置cookie
        //默认的恢复策略
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        InputStream is = null;
        OutputStream os = null;
        String msg= null , str = null;
        try{
            //开始执行getMethod
            int statusCode = httpClient.executeMethod(getMethod),byteread = 0;
            if (statusCode != HttpStatus.SC_OK)  System.err.println("Method failed:" + getMethod.getStatusLine());
            else {
                //流读取内容
                //注意获取相关参数【具体可以查看实现类】
                String [] rsparams =getMethod.getResponseHeader("content-disposition").toString().split(";")
                for(String strs  : rsparams) {
                    if(strs!= null && strs.indexOf("filename")!=-1) {//这里建议抓包获取你的文件名
                        faname = strs.split("=")[1];
                        break;
                    }
                }
                faname = faname.trim();
                File file = new File(outurl+faname);
                if(!file.exists()) { file.createNewFile();  }//文件判断

                is = getMethod.getResponseBodyAsStream();
                os = new FileOutputStream(file);
                byte[] buffer = new byte[1204];//缓冲
                while ((byteread = is.read(buffer)) != -1) {//判断是否有数据,并读取
                    os.write(buffer, 0, byteread);//输出
                }
                os.flush();
            }
        }  catch(Exception e){//网络异常
        }finally{
            //释放连接
            getMethod.releaseConnection();
            if(is != null )  is.close();
            if(os != null)  os.close();
        }
        return msg;
    }
    //测试
 	String url = "http://localhost:8082/weaver/weaver.file.FileDownload?fileid=1983&requestid=-1";
    String strmsg =  downloadNetLoad(url, "d:"+File.separator, "load.txt");

上传:

同一个域

如果在同一个域,我们可以通过共享文件的方式使用smb(SMB(全称是Server Message Block)是一个协议名,它能被用于Web连接和客户端与服务器之间的信息沟通。)协议进行数据传输


  // 共享文件夹所在服务器ip
    private static String USER_DOMAIN = "10.10.32.12/weaver";
    //访问用户
    private static String USER_ACCOUNT = "test";
    //访问密码
    private static String USER_PWS = "test";
    //共享文件夹地址
    private static final String shareDirectory = "smb://10.10.32.12/weaver/dir";
    //字节长度
    private static final int byteLen = 1024;
    /**
     *
     * @Title smbPut
     * @Description 向共享目录上传文件
     * @Param shareDirectory 共享目录
     * @Param localFilePath 本地目录中的文件路径
     * @date 2019-01-10 20:16
     */
    public static void smbPut(String shareDirectory, File localFile) {
        InputStream in = null;
        OutputStream out = null;
        try {
            String fileName = localFile.getName();
            // 域服务器验证
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT, USER_PWS);
            SmbFile remoteFile = new SmbFile(shareDirectory + "/" + fileName, auth);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[byteLen];
            while (in.read(buffer) != -1) {
                out.write(buffer);
                buffer = new byte[byteLen];
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

服务器有上传接口

上传抓包:
在这里插入图片描述

	public String Cookie = "JSESSIONID=abcl9g_Yt1I7W6eBpzb6w;";
	//测试
	public void testUpload() {
        String filepath = "d://load.txt";
        Map<String, String> textMap = new HashMap<String, String>();
        textMap.put("name", "text.txt");
        textMap.put("isFirstUploadFile", "9");
        Map<String, String> fileMap = new HashMap<String, String>();
        //随便写入的参数
        fileMap.put("userfile", filepath);
        String upurl = "http://localhost:8082/api/workflow/reqform/docUpload?category=41,57,87&workflowid=219&listType=list&authStr=dmlld0NoYWluPTE1MjQwNTZ8bWFpbmlkPTE1MjQwNTZ8&authSignatureStr=16447a7f90b714a8508ce750dbf4283f&f_weaver_belongto_userid=1&f_weaver_belongto_usertype=0&wfTestStr= HTTP/1.1: ";
        String ret = formUpload(upurl, textMap, fileMap);
    }
    
    public String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) {
        HttpURLConnection conn = null;
        // 换行符
        final String newLine = "\r\n";
        final String boundaryPrefix = "--";
        String BOUNDARY = "----WebKitFormBoundaryCXD2k7zA9EPBHp0o"; // boundary就是request头和上传文件内容的分隔符
        try {
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(2000);
            conn.setReadTimeout(3000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Cookie", Cookie);//session数据
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 " +
                    "(KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
            conn.setRequestProperty("Content-Type",  "multipart/form-data; boundary=" + BOUNDARY);

            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            // text
            if (textMap != null) {
                StringBuffer strBuf = new StringBuffer();
                Iterator iter = textMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String inputName = (String) entry.getKey();
                    String inputValue = (String) entry.getValue();
                    if (inputValue == null)  continue;
                    strBuf.append(newLine).append(boundaryPrefix).append(BOUNDARY).append(newLine);
                    strBuf.append("Content-Disposition: form-data; name=\""+ inputName + "\""+newLine+newLine);
                    strBuf.append(inputValue);
                }
                out.write(strBuf.toString().getBytes());
            }

            // file
            if (fileMap != null) {
                Iterator iter = fileMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String inputName = (String) entry.getKey(),
                            inputValue = (String) entry.getValue();
                    if (inputValue == null)  continue;
                    File file = new File(inputValue);
                    String filename = file.getName();
                    String contentType = new MimetypesFileTypeMap().getContentType(file);
                    if (filename.endsWith(".png")) {contentType = "image/png"; }
                    if (contentType == null || contentType.equals("")) {
                        contentType = "application/octet-stream";
                    }

                    StringBuffer strBuf = new StringBuffer();
                    strBuf.append(newLine).append(boundaryPrefix).append(BOUNDARY).append(newLine);
                    strBuf.append("Content-Disposition: form-data; name=\""+ inputName + "\"; filename=\"" + filename+ "\""+newLine);
                    strBuf.append("Content-Type:" + contentType + newLine+newLine);
                    out.write(strBuf.toString().getBytes());

                    DataInputStream in = new DataInputStream( new FileInputStream(file));
                    int bytes = 0;
                    byte[] bufferOut = new byte[1024];
                    while ((bytes = in.read(bufferOut)) != -1) {
                        out.write(bufferOut, 0, bytes);
                    }
                    in.close();
                }
            }

            byte[] endData = (boundaryPrefix + BOUNDARY + boundaryPrefix +newLine).getBytes();
            out.write(endData);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return "";
    }


附:
.net4.5下载连接:https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=30653
.fiddler下载连接(包含.Net4.5):
链接:https://pan.baidu.com/s/1ug1fziQ6zhoisxjsf0wAQw
提取码:3arb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的HttpURLConnection类来模拟表单上传文件。以下是一个简单的示例代码: ```java import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void main(String[] args) throws IOException { String url = "http://example.com/upload"; // 替换为实际的上传地址 String filePath = "/path/to/file"; // 替换为实际的文件路径 URL uploadUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) uploadUrl.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); // 设置请求参数 File file = new File(filePath); String fileName = file.getName(); String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; String lineBreak = "\r\n"; String postData = "--" + boundary + lineBreak + "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + lineBreak + "Content-Type: application/octet-stream" + lineBreak + lineBreak; // 发送请求 OutputStream outputStream = connection.getOutputStream(); outputStream.write(postData.getBytes()); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } String endData = lineBreak + "--" + boundary + "--" + lineBreak; outputStream.write(endData.getBytes()); // 获取响应 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 上传成功 } else { // 上传失败 } // 关闭连接 outputStream.close(); connection.disconnect(); } } ``` 在上面的代码中,我们首先创建一个URL对象,然后打开一个HttpURLConnection连接。我们设置请求方法为POST,同时将其设置为允许输出。我们还设置了请求头中的Content-Type字段,以指示我们正在发送一个multipart/form-data类型的表单请求。 接着,我们设置请求参数。我们将要上传的文件读入一个byte数组中,并将其作为请求主体发送。我们还设置了一个分隔符,以便服务器能够识别请求中的不同部分。 最后,我们发送请求,并读取响应。如果响应代码为HTTP_OK,表示上传成功。如果上传失败,我们可以根据响应代码采取不同的措施。最后,我们关闭连接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值