有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当文件包含“类”时,使用Java之外的HTTP进行eclipse上载不起作用

我正试图通过HTTP从Eclipse插件客户端上传文件。当我使用HTTPrequest的getOutputStream添加文件时,包括字符串“class”的文本文件的连接失败。如果文件中没有“class”,则连接正常。我

我不知道为什么会这样。在服务器端,文件将被上传、链接缩短并可以打开。每件事都按照它应该的方式进行,除了文本中包含“类”


共 (1) 个答案

  1. # 1 楼答案

    确保使用表单多部分编码POST,如下例所示:

        final String CHARSET = "ISO-8859-1";
        final String CRLF = "\r\n";
        String formFieldName = "uploadfile";
        String fileName = "upload.jpeg";
        String fileContentType = "image/jpeg";
        URL url = new URL("http://localhost");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true); // if input is expected
        String boundary = "          "+System.currentTimeMillis();
        StringBuilder postData = new StringBuilder();
        postData.append(" ").append(boundary).append(CRLF);
        postData.append("Content-Disposition: form-data; name=\"").append(formFieldName).append("\"; filename=\"").append(fileName).append("\"").append(CRLF);
        postData.append("Content-Type: ").append(fileContentType).append(CRLF);
        postData.append(CRLF).append(new String(bytes, CHARSET)).append(CRLF);
        postData.append(" ").append(boundary).append(" ").append(CRLF);   
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
        connection.setRequestProperty("Content-Length", Integer.toString(postData.length()));
        connection.setFixedLengthStreamingMode(postData.length());
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), CHARSET);
        out.write(postData.toString());
        out.close();
        assert(connection.getResponseCode() == HttpURLConnection.HTTP_OK);
        // if input is expected
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), CHARSET));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    

    干杯, 麦克斯