用于上传文件的Java小程序

用于上传文件的Java小程序,java,forms,post,applet,Java,Forms,Post,Applet,我正在寻找一个Java小程序从客户端机器读取一个文件,并为PHP服务器上传创建一个POST请求 服务器上的PHP脚本应该以表单提交的形式接收文件作为正常文件上传。 我正在使用以下代码。文件内容被传递到PHP脚本 但它们没有正确地转换为图像 //uploadURL will be a url of PHP script like // http://www.example.com/uploadfile.php URL url = new URL(uploadURL); HttpURLConnec

我正在寻找一个Java小程序从客户端机器读取一个文件,并为PHP服务器上传创建一个POST请求

服务器上的PHP脚本应该以表单提交的形式接收文件作为正常文件上传。 我正在使用以下代码。文件内容被传递到PHP脚本 但它们没有正确地转换为图像

//uploadURL will be a url of PHP script like
// http://www.example.com/uploadfile.php

URL url = new URL(uploadURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);

InputStream is = new FileInputStream("C://img.jpg");
OutputStream os = con.getOutputStream();
byte[] b1 = new byte[10000000];
int n;
while((n = is.read(b1)) != -1) {
os.write("hello" , 0, 5);
test += b1;

}
con.connect();

我建议你去看看。这是一个将照片上传到PHP后端的开源项目。它比您可能需要的功能更全面,但是您应该能够相当容易地根据您的需要修改代码


你也可以看看。它功能不全,但它是开源的,能够完成这项任务。

这里有一些代码可以帮助您,它来自我的一个老项目,删除了一些不相关的东西,值得一看。基本上,我认为您问题中的代码缺少HTTP协议所需的某些部分

public class UploaderExample
{
    private static final String Boundary = "--7d021a37605f0";

    public void upload(URL url, List<File> files) throws Exception
    {
        HttpURLConnection theUrlConnection = (HttpURLConnection) url.openConnection();
        theUrlConnection.setDoOutput(true);
        theUrlConnection.setDoInput(true);
        theUrlConnection.setUseCaches(false);
        theUrlConnection.setChunkedStreamingMode(1024);

        theUrlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
                + Boundary);

        DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream());

        for (int i = 0; i < files.size(); i++)
        {
            File f = files.get(i);
            String str = "--" + Boundary + "\r\n"
                       + "Content-Disposition: form-data;name=\"file" + i + "\"; filename=\"" + f.getName() + "\"\r\n"
                       + "Content-Type: image/png\r\n"
                       + "\r\n";

            httpOut.write(str.getBytes());

            FileInputStream uploadFileReader = new FileInputStream(f);
            int numBytesToRead = 1024;
            int availableBytesToRead;
            while ((availableBytesToRead = uploadFileReader.available()) > 0)
            {
                byte[] bufferBytesRead;
                bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead]
                        : new byte[availableBytesToRead];
                uploadFileReader.read(bufferBytesRead);
                httpOut.write(bufferBytesRead);
                httpOut.flush();
            }
            httpOut.write(("--" + Boundary + "--\r\n").getBytes());

        }

        httpOut.write(("--" + Boundary + "--\r\n").getBytes());

        httpOut.flush();
        httpOut.close();

        // read & parse the response
        InputStream is = theUrlConnection.getInputStream();
        StringBuilder response = new StringBuilder();
        byte[] respBuffer = new byte[4096];
        while (is.read(respBuffer) >= 0)
        {
            response.append(new String(respBuffer).trim());
        }
        is.close();
        System.out.println(response.toString());
    }

    public static void main(String[] args) throws Exception
    {
        List<File> list = new ArrayList<File>();
        list.add(new File("C:\\square.png"));
        list.add(new File("C:\\narrow.png"));
        UploaderExample uploader = new UploaderExample();
        uploader.upload(new URL("http://systemout.com/upload.php"), list);
    }

}
公共类上载示例
{
私有静态最终字符串边界=“--7d021a37605f0”;
公共无效上载(URL、列表文件)引发异常
{
HttpURLConnection theUrlConnection=(HttpURLConnection)url.openConnection();
URLConnection.setDoOutput(true);
URLConnection.setDoInput(true);
URLConnection.setUseCaches(false);
URLConnection.setChunkedStreamingMode(1024);
setRequestProperty(“内容类型”,“多部分/表单数据;边界=”
+边界);
DataOutputStream httpOut=新的DataOutputStream(theUrlConnection.getOutputStream());
对于(int i=0;i+边界+”\r\n
+“内容处理:表单数据;名称=\”文件“+i+”\”文件名=\”+f.getName()+“\”\r\n”
+“内容类型:image/png\r\n”
+“\r\n”;
write(str.getBytes());
FileInputStream uploadFileReader=新的FileInputStream(f);
int numBytesToRead=1024;
int可用字节存储AD;
而((availableBytesToRead=uploadFileReader.available())>0)
{
字节[]缓冲字节读取;
bufferBytesRead=availableBytesToRead>=numBytesToRead?新字节[numBytesToRead]
:新字节[availableBytesToRead];
uploadFileReader.read(bufferBytesRead);
httpOut.write(bufferBytesRead);
httpOut.flush();
}
httpOut.write((“--”+Boundary+“--\r\n”).getBytes();
}
httpOut.write((“--”+Boundary+“--\r\n”).getBytes();
httpOut.flush();
httpOut.close();
//读取并解析响应
InputStream=theUrlConnection.getInputStream();
StringBuilder响应=新建StringBuilder();
字节[]respBuffer=新字节[4096];
而(is.read(respBuffer)>=0)
{
append(新字符串(respBuffer.trim());
}
is.close();
System.out.println(response.toString());
}
公共静态void main(字符串[]args)引发异常
{
列表=新的ArrayList();
添加(新文件(“C:\\square.png”);
添加(新文件(“C:\\窄带.png”);
UploaderExample uploader=新的UploaderExample();
uploader.upload(新URL(“http://systemout.com/upload.php(附表),;
}
}

您实际在哪里将输入写入输出?据我所知,您正在向连接写入的唯一内容是字符串“hello”。该项目包括一个小程序。单一源是标准应用程序、浏览器外小程序和浏览器内小程序。看见