使用PHP上传Java文件:不工作
我正试图在点击按钮时上传一个图像文件。我在iOS上使用了相同的php脚本,它似乎工作得很好。我添加了一个用于上传文件的代码。我怀疑我没有正确地传递图像文件路径。我实际上添加了保存图像文件的方法。我只是将其保存为“sign.jpeg”。我没有看到指向uploadfile(视图)方法的单击按钮上的图像 实现文件上传的方法使用PHP上传Java文件:不工作,java,php,android,Java,Php,Android,我正试图在点击按钮时上传一个图像文件。我在iOS上使用了相同的php脚本,它似乎工作得很好。我添加了一个用于上传文件的代码。我怀疑我没有正确地传递图像文件路径。我实际上添加了保存图像文件的方法。我只是将其保存为“sign.jpeg”。我没有看到指向uploadfile(视图)方法的单击按钮上的图像 实现文件上传的方法 public void upload(String selectedPath) throws IOException { HttpURLConnection connec
public void upload(String selectedPath) throws IOException {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = selectedPath;
String urlServer = "http://localhost:8888/uploadJava.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(
pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream
.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";name=\""
+ "sign.jpeg" + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
/*int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();*/
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
fileInputStream.close();
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
}
}
上载文件的php脚本
<?php
$target_path = "./images/";
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
error_log("Upload File >>" . $target_path . $_FILES['error'] . " \r\n", 3,
"Log.log");
error_log("Upload File >>" . basename($_FILES['uploadedfile']['name']) . " \r\n",
3, "Log.log");
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file " . basename($_FILES['uploadedfile']['name']) .
" has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>
图像保存方法
private void saveImage(String filename, Bitmap b, Context ctx){
try {
ObjectOutputStream oos;
FileOutputStream out;// = new FileOutputStream(filename);
out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(out);
b.compress(Bitmap.CompressFormat.PNG, 100, oos);
oos.close();
oos.notifyAll();
out.notifyAll();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
是的,Internet权限添加到AndroidManifest.xml中,如下所示
<uses-permission android:name="android.permission.INTERNET"/>
知道上面的代码有什么问题吗?首先执行以下操作:
File ourFile = new File(pathToOurFile);
int ourFileLength = (int)ourFile.length();
bufferSize = Math.min(ourFileLength, maxBufferSize);
然后我不得不承认几乎从未使用过available()
。
写的应该是#字节读
for (;;) (
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
if (bytesRead <= 0) {
break;
}
outputStream.write(buffer, 0, bytesRead);
}
(;;)的(
int bytesRead=fileInputStream.read(buffer,0,bufferSize);
如果(bytesRead由于您是通过HTTP向php发送文件,您可以尝试以下方法:
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("yourdomain.com");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
MultipartEntity entity = new MultipartEntity();
entity.addPart("uploadedfile", new FileBody(new File ("absolute_path_to_file")));
httpPost.setEntity(entity);
String resp = client.execute(httpPost, responseHandler);
} catch (IOException e) {
Log.d("log", e.getMessage(), e);
}
HttpClient=newdefaulthttpclient();
HttpPost-HttpPost=newhttppost(“yourdomain.com”);
ResponseHandler ResponseHandler=新BasicResponseHandler();
试一试{
多方实体=新多方实体();
addPart(“uploadedfile”,新文件体(新文件(“绝对路径到文件”));
httpPost.setEntity(实体);
String resp=client.execute(httpPost,responseHandler);
}捕获(IOE异常){
Log.d(“Log”,e.getMessage(),e);
}
对于这种方法,您需要在构建路径中使用httpime和httpclient-lib:
for (;;) (
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
if (bytesRead <= 0) {
break;
}
outputStream.write(buffer, 0, bytesRead);
}
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("yourdomain.com");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
MultipartEntity entity = new MultipartEntity();
entity.addPart("uploadedfile", new FileBody(new File ("absolute_path_to_file")));
httpPost.setEntity(entity);
String resp = client.execute(httpPost, responseHandler);
} catch (IOException e) {
Log.d("log", e.getMessage(), e);
}