如果我将filedata声明为file(参见图3),它就会工作。我试着用Java来做,但我找不到方法。我尝试了很多网络上的代码片段,但当我运行它们时,最多只能得到500个错误。
到目前为止我得到的代码是
package api_post;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class API_POST {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String charset = "UTF-8";
File uploadFile1 = new File("[fileWithPath]");
String requestURL = "";
requestURL = "[URL]";
try {
MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addFormField("username", "[user]");
multipart.addFormField("password", "[pwd]");
multipart.addFormField("owner", "[owner]");
multipart.addFormField("destination", "[destination]");
multipart.addFilePart("fileData", uploadFile1);
List<String> response = multipart.finish();
System.out.println("SERVER REPLIED:");
for (String line : response) {
System.out.println(line);
}
} catch (IOException ex) {
System.err.println(ex);
}
}
public static class MultipartUtility {
private final String boundary;
private static final String LINE_FEED = "\r\n";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
public MultipartUtility(String requestURL, String charset) throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
}
/**
* Adds a form field to the request
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=" + charset).append(
LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
/**
* Adds a upload file section to the request
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile) throws IOException {
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append(
"Content-Disposition: form-data; name=\"" + fieldName
+ "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
writer.append(
"Content-Type: "
+ URLConnection.guessContentTypeFromName(fileName))
.append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
/**
* Adds a header field to the request.
* @param name - name of the header field
* @param value - value of the header field
*/
public void addHeaderField(String name, String value) {
writer.append(name + ": " + value).append(LINE_FEED);
writer.flush();
}
/**
* Completes the request and receives response from the server.
* @return a list of Strings as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public List<String> finish() throws IOException {
List<String> response = new ArrayList<String>();
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
System.out.println("Entrato");
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}}}
有人能帮我吗?
我刚刚发现在postman我可以看到调用的java源代码。我用了那个,现在它工作了。
问题内容: 我正在为JEE5 Web服务编写单元测试。Web服务的行为取决于文件中设置的属性。因此,我想将我分成一个恒定的部分和一个在两次测试之间更改的部分。 为了查看是否确实可行,我尝试查看是否可以拆分该属性。根据一些说明,我发现了以下几点: web.xml fragment.xml 但是,我在文件上收到验证错误: 必须为元素类型“ web-app”声明属性“ version”。[7] 必须为元
我有一个结构如下的maven项目: 我希望maven jar插件在构建jar文件时打包到数据库文件夹中。我尝试过包含这里概述的内容:https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html,但它找不到目录“../database”(或../../database或../../../database
问题内容: 我记得前一段时间使用eclipse或netbeans时,我能够轻松地将源文件包含在jar文件中。我已移至intellij 10社区版,发现自己需要做同样的事情。有什么提示吗? 问题答案: 我想到了… 单击“输出布局”选项卡中的“ +”按钮->包含目录内容->选择您的src目录。做到了
问题内容: 我有一个包含几个JavaScript函数的JavaScript文件(extension ,不是)。 我想从一个JavaScript函数中仅包含几个PHP函数的PHP文件中调用其中一个PHP函数。 那可能吗? 我需要在文件中“包含” 包含PHP函数的文件吗? 我该怎么办? 例如,假设我有一个名为myLib.php的文件,其中包含一个带有两个参数(和)的函数。然后,我有一个包含名为的函数的
我还使用tkinter用Python3.7编写了一个程序。因为我使用的是外部图片,所以当我将所有内容编译成一个exe时,我需要包含它们。我尝试过执行,但仍然出现以下错误: _特金特。Tcl错误:无法打开“files/bg.png”:没有这样的文件或目录 代码如下: 我做错了什么?我也尝试过添加二进制文件,将该文件添加到我的spec文件中。我真的搞不懂!
如果我直接编写上面的代码,上面的代码可以正常工作,我试图在其他文件和主文件中编写此代码,我做了 我如何能包括它有没有办法包括,也想知道一个更好的方式来写上述声明。 谢谢你