如何解决在使用Java上传文件的过程中,如何在发布请求中添加多个参数
|| 我终于使用核心Java上传了文件。这是我的代码:import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class FileUpload {
public static final String C_EMAIL = \"email@gmail.com\";
public static final String C_PASSWORD = \"password\";
public static void main(String args[]) {
try {
// Send data
String url = \"http://localhost/upload.php\";
File binaryFile = new File(\"C:\\\\wamp\\\\www\\\\image.png\");
// Just generate some unique random value.
String boundary = Long.toHexString(System.currentTimeMillis());
// Line separator required by multipart/form-data.
String CRLF = \"\\r\\n\";
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty(\"Content-Type\",\"multipart/form-data; boundary=\" + boundary);
PrintWriter writer = null;
try {
OutputStream output = connection.getOutputStream();
// true = autoFlush,important!
writer = new PrintWriter(
new OutputStreamWriter(output,\"UTF-8\"),true);
// Send normal param.
writer.append(\"--\" + boundary).append(CRLF);
writer.append(\"Content-Disposition: form-data; name=\\\"email\\\"\")
.append(CRLF);
writer.append(\"Content-Type: text/plain; charset=\" + \"UTF-8\")
.append(CRLF);
writer.append(CRLF);
writer.append(C_EMAIL).append(CRLF).flush();
// Send binary file.
writer.append(\"--\" + boundary).append(CRLF);
writer.append(
\"Content-Disposition: form-data;\"
+ \"name=\\\"file\\\"; filename=\\\"\" + binaryFile.getName()
+ \"\\\"\").append(CRLF);
writer.append(\"Content-Type: \"
+ URLConnection.guessContentTypeFromName(binaryFile
.getName()) + CRLF);
writer.append(\"Content-Transfer-Encoding: binary\").append(CRLF);
System.out.println(\"Content transfer set\");
writer.append(CRLF).flush();
InputStream input = null;
try {
System.out.println(\"Entered try\");
input = new FileInputStream(binaryFile);
byte[] buffer = new byte[1024];
System.out.println(\"Before for loop\");
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer,length);
}
output.flush(); // Important! Output cannot be closed.
// Close of writer will close output as well.
System.out.println(\"output flushed\");
} finally {
if (input != null) try {
input.close();
} catch (IOException logOrIgnore) {}
}
writer.append(CRLF).flush(); // CRLF is important! It indicates
// end of binary boundary.
// End of multipart/form-data.
writer.append(\"--\" + boundary + \"--\").append(CRLF);
} finally {
if (writer != null) writer.close();
}
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
在我的PHP脚本中,我能够获取要上传的文件以及带有其值的\'email \'参数。
但是,我不知道如何再传递一个附加参数,即\“ password \”。
有人可以建议我吗?
解决方法
使用提供的“ 1” API或更方便的Apache HttpComponents Client。
也可以看看:
使用java.net.URLConnection触发和处理HTTP请求-在底部附近可以找到一个代码段。
HttpClient示例-在底部,您可以找到一个“多部分编码的请求实体”示例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。