在android中上载文本文件时出错(服务器无法理解浏览器发送的请求)
" 尝试 { 字符串upurl=; 字符串路径=getFilesDir().getPath(); String filename=dq.fetchfilename(); filename=filename.replaceFirst(“M”、“T”); path=path.concat(“/”+文件名) 安卓代码-在android中上载文本文件时出错(服务器无法理解浏览器发送的请求),android,Android," 尝试 { 字符串upurl=; 字符串路径=getFilesDir().getPath(); String filename=dq.fetchfilename(); filename=filename.replaceFirst(“M”、“T”); path=path.concat(“/”+文件名) 安卓代码- public class UploadFileToServer extends AsyncTask<Object, String, Object> { URL conn
public class UploadFileToServer extends AsyncTask<Object, String, Object>
{
URL connectURL;
String params;
String responseString;
String fileName;
byte[] dataToServer;
FileInputStream fileInputStream;
private int serverResponseCode;
private String serverResponseMessage;
private static final String TAG = "Uploader";
public void setUrlAndFile(String urlString, File fileName)
{
Log.d(TAG,"StartUploader");
try
{
fileInputStream = new FileInputStream(fileName);
connectURL = new URL(urlString);
}
catch(Exception e)
{
e.getStackTrace();
publishProgress(e.toString());
}
this.fileName = fileName.getAbsolutePath()+".txt";
}
synchronized void doUpload()
{
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
Log.d(TAG,"lv1");
try
{
Log.d(TAG,"doUpload");
publishProgress("Uploading...");
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition:form-data; name=\"Uploaded\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.d(TAG,"LvA");
Log.d(TAG,twoHyphens + boundary + lineEnd + ";Content-Disposition:form-data; name=\"Uploaded\";filename=\"" + fileName + "\"" + lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer,0, bufferSize);
Log.d(TAG,"LvB");
while(bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
int ch;
Log.d(TAG,"LvC");
StringBuffer buff = new StringBuffer();
while((ch=is.read()) != -1)
{
buff.append((char)ch);
}
// publishProgress(buff.toString());
dos.close();
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
serverResponseMessage = conn.getResponseMessage();
// Log.d(TAG,"Buffer "+buff.toString());
Log.d(TAG,"Server Response "+serverResponseMessage);
}
catch(Exception e)
{
e.getStackTrace();
publishProgress(e.toString());
}
}
@Override
protected Object doInBackground(Object... arg0)
{
Log.d(TAG,"lv1a");
doUpload();
Log.d(TAG,"Uploading Completed! Path: "+connectURL);
return null;
}
protected void onProgressUpdate(String... progress)
{
//this.info.setText(progress[0]);
Log.d("Progress", progress[0]);
}
}
公共类UploadFileToServer扩展异步任务
{
URL-connectURL;
字符串参数;
弦乐;
字符串文件名;
字节[]dataToServer;
FileInputStream FileInputStream;
专用int服务器响应代码;
私有字符串服务器响应消息;
私有静态最终字符串TAG=“Uploader”;
public void setUrlAndFile(字符串urlString,文件名)
{
Log.d(标签“StartUploader”);
尝试
{
fileInputStream=新的fileInputStream(文件名);
connectURL=新URL(urlString);
}
捕获(例外e)
{
e、 getStackTrace();
出版进度(如toString());
}
this.fileName=fileName.getAbsolutePath()+“.txt”;
}
同步的void doUpload()
{
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
日志d(标签“1级”);
尝试
{
Log.d(标记为“双重负载”);
出版进度(“上传…”);
HttpURLConnection conn=(HttpURLConnection)connectURL.openConnection();
conn.setDoInput(真);
连接设置输出(真);
conn.SETUSECHACHES(假);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
DataOutputStream dos=新的DataOutputStream(conn.getOutputStream());
写字节(两个连字符+边界+行结束);
writeBytes(“内容处理:表单数据;名称=\”上传\“文件名=\”+文件名+“\”+行结束”);
dos.writeBytes(lineEnd);
日志d(标签“LvA”);
Log.d(标记,两个连字符+边界+lineEnd+,“内容处理:表单数据;名称=\”上传\“;文件名=\”+文件名+“\”+lineEnd);
int bytesavable=fileInputStream.available();
int maxBufferSize=1024;
int bufferSize=Math.min(字节可用,maxBufferSize);
字节[]缓冲区=新字节[bufferSize];
int bytesRead=fileInputStream.read(buffer,0,bufferSize);
日志d(标签“LvB”);
而(字节读取>0)
{
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
dos.writeBytes(lineEnd);
写字节(两个连字符+边界+两个连字符+行结束);
fileInputStream.close();
dos.flush();
InputStream is=conn.getInputStream();
int-ch;
日志d(标签“LvC”);
StringBuffer buff=新的StringBuffer();
而((ch=is.read())!=-1)
{
buff.append((char)ch);
}
//publishProgress(buff.toString());
dos.close();
//来自服务器的响应(代码和消息)
serverResponseCode=conn.getResponseCode();
serverResponseMessage=conn.getResponseMessage();
//Log.d(标记“Buffer”+buff.toString());
Log.d(标记“服务器响应”+服务器响应消息);
}
捕获(例外e)
{
e、 getStackTrace();
出版进度(如toString());
}
}
@凌驾
受保护对象doInBackground(对象…arg0)
{
日志d(标签“lv1a”);
双倍体();
Log.d(标记“上传完成”!路径:+connectURL);
返回null;
}
受保护的void onProgressUpdate(字符串…进度)
{
//this.info.setText(progress[0]);
日志d(“进度”,进度[0]);
}
}
PHP代码-
< ?php
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
? >
<?php
$file_path=“uploads/”;
$file_path=$file_path.basename($_FILES['upload_file']['name']);
如果(移动上传的文件($上传的文件['Upload\u file']['tmp\u name'],$file\u path)){
呼应“成功”;
}否则{
回应“失败”;
}
? >
你也可以查一下
400错误请求错误请求您的浏览器发送了一个此服务器无法理解的请求。
此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误。conn.setDoInput(true);conn.setDoOutput(true);//您传递给connectURL.com的内容(以http为前缀)在此行处出现null指针异常
< ?php
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
? >