所以,我无法摆脱这个问题:
我需要将我的Android应用程序(API 8)中的XML
文件和.jpg
文件上传到HTTP服务器(使用IIS 7.5的win 2008服务器)。我已启用PUT
动词并已卸载WebDav
&从以前的搜索中建议的webdav方法。
另外,我不确定我是否正确地服务器端,因为我无法得到任何回复。
这是我的代码
URL fileurl = new URL("Server Upload Path");
HttpURLConnection urlConnection = (HttpURLConnection) fileurl
.openConnection();
urlConnection.setRequestMethod("PUT");
urlConnection.setDoOutput(true);
urlConnection.connect();
OutputStream os = urlConnection.getOutputStream();
File upFile = new File("My Local File");
//I'm sure the file exists
FileInputStream fis = new FileInputStream(upFile);
BufferedInputStream bfis = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int bufferLength = 0;
// now, read through the input buffer and write the contents to the
// file
while ((bufferLength = bfis.read(buffer)) > 0) {
os.write(buffer, 0, bufferLength);
}
很抱歉,如果我忘记了一些您可能需要帮助的信息。我是android和IIS
的新手。
答案 0 :(得分:-1)
为什么不尝试标准的多部分文件上传请求(基于POST而不是PUT):
final static String MULTIPART_BOUNDARY = "------------------563i2ndDfv2rTHiSsdfsdbouNdArYfORhxcvxcvefj3q2f";
public static void sendFileToServer(String url, File logFiles) {
HttpURLConnection connection = null;
OutputStream os = null;
DataInputStream is = null;
try {
StringBuilder fullUrl = new StringBuilder(url);
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MULTIPART_BOUNDARY);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.connect();
os = new BufferedOutputStream(connection.getOutputStream());
if(os != null) {
os.write(("--" + MULTIPART_BOUNDARY + EOL).getBytes());
os.write(String.format("Content-Disposition:form-data;name=\"UploadedFile\";filename=\"%s\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n", UPLOADED_FILE_NAME).getBytes());
// Upload file(s) data here and send
os.write((EOL + "--" + MULTIPART_BOUNDARY + "--" + EOL + EOL).getBytes());
os.flush();
os.close();
os = null;
}
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// Process server response
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (Exception e1) {
} finally {
try {
if(os != null) {
os.close();
}
} catch (IOException e) {
Log.e(TAG, "sendFileToServer exception: close OutputStream", e);
}
try {
if(is != null) {
is.close();
}
} catch (IOException e) {
Log.e(TAG, "sendFileToServer exception: close InputStream", e);
}
if(connection != null) {
connection.disconnect();
}
}
}