小编use*_*701的帖子

带有HTTP上载的Java ProgressBar不会更新

我想让我jProgressBar在期间更新它的价值HTTP File Upload.我是Java新手,我不确定我做的是正确的事情,这是我的代码:

private static final String Boundary = "--7d021a37605f0";

public void upload(URL url, File f) 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());


        String str = "--" + Boundary + "\r\n"
                   + "Content-Disposition: form-data;name=\"file1\"; 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;
        jProgressBar1.setMaximum(uploadFileReader.available());
        while ((availableBytesToRead = uploadFileReader.available()) …
Run Code Online (Sandbox Code Playgroud)

java swing file-upload jprogressbar java-io

2
推荐指数
1
解决办法
673
查看次数

标签 统计

file-upload ×1

java ×1

java-io ×1

jprogressbar ×1

swing ×1