请问;如何用POST方法上传文件?
package tw.android;
import java.io.File;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static void uploadFileClient()
{
String targetURL = "http://api.yeelink.net/v1.0/devices";// 上传指定URL
File SDFile = android.os.Environment.getExternalStorageDirectory();
File targetFile = new File(SDFile.getAbsolutePath() + File.separator + "datafile.txt");// 指定上传文件
PostMethod filePost = new PostMethod(targetURL);
try
{
// 通过以下方法可以模拟页面参数提交
// filePost.setParameter("name", "中文");
// filePost.setParameter("pass", "1234");
byte[] buffer = new byte[1024];
Part[] parts =
{ new FilePart(targetFile.getName() + System.currentTimeMillis(),
targetFile) };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new DefaultHttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK)
{
System.out.println("上传成功");
// 上传成功
} else
{
System.out.println("上传失败");
// 上传失败
}
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
filePost.releaseConnection();
}
}
}
加红的地方这么报错的
Multiple markers at this line
- PostMethod cannot be resolved to
a type
- PostMethod cannot be resolved to
a type
怎么弄啊,求啊
[解决办法]
没有PostMethod这个类吧?
应该用 org.apache.http.client.methods.HttpPost 代替。