我想将图像上传到网络服务器。因为我已经编写了代码,但是当我将文件上传到网络服务器而不是文件名时,我将获得图像路径。
码
protected String sendFile(final Context context, final String url, final String params, String fileName, final File f) {
DataOutputStream dos = null;
int bytesRead = 0, bytesAvailable = 0, bufferSize;
int maxBufferSize = 1024;
byte[] buffer;
String serverResponseMessage = null;
String uploaded_file = fileName;
URL url1 = null;
try {
FileInputStream fileInputStream = new FileInputStream(f);
url1 = new URL(url + "?" + params);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("uploaded_file", fileName);
fileName = String.valueOf(f);
//connection.setRequestProperty ("uploaded_file", "sample");
dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"hello" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.close();
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
StringBuffer b;
int ch;
InputStream is;
is = connection.getInputStream();
// retrieve the response from server
b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String response = b.toString();
Log.e("Response", "" + response);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e("Error message", "" + e);
}
return serverResponseMessage;
}
protected File saveFile(Context context, int value, String name) {
File directory = new File(Environment.getExternalStorageDirectory() + "/pocketDocs/Camera/Gallery/Others");
if (!directory.exists()) {
directory.mkdirs();
}
if (value == 0) {
f = new File(Environment.getExternalStorageDirectory() + "/pocketDocs/Gallery", name);
try {
FileOutputStream out = new FileOutputStream(f);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return f;
}
}
当我使用
时fileName = String.valueOf(f);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"hello" + fileName + "\"" + lineEnd);
在服务器端,我将文件名作为图像路径。
当我不使用时
fileName = String.valueOf(f);
我得到了正确的文件名,但我没有得到图片的扩展名。
现在我怎么能解决这个问题,因为peoblem在这一行
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"hello" + fileName + "\"" + lineEnd);
答案 0 :(得分:0)
您在文件中使用String.valueOf()
来获取其名称,它返回一个空字符串,因此无效。
相反,您必须使用file.getName()
获取文件名。