android 上传文件到服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 上传文件到服务器相关的知识,希望对你有一定的参考价值。
Android客户端
import java.io.DataOutputStream; |
import java.io.FileInputStream; |
import java.io.InputStream; |
import java.net.HttpURLConnection; |
import android.app.Activity; |
import android.app.AlertDialog; |
import android.content.DialogInterface; |
import android.os.Bundle; |
import android.view.View; |
import android.widget.Button; |
import android.widget.TextView; |
public class PhotoUpload extends Activity { |
private String newName = "image.jpg" ; |
private String uploadFile = "/sdcard/image.JPG" ; |
public void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
mText1 = (TextView) findViewById(R.id.myText2); |
mText1.setText(uploadFile); |
mText2 = (TextView) findViewById(R.id.myText3); |
mText2.setText(actionUrl); |
mButton = (Button) findViewById(R.id.myButton); |
mButton.setOnClickListener( new View.OnClickListener() { |
public void onClick(View v) { |
private void uploadFile() { |
String twoHyphens = "--" ; |
String boundary = "*****" ; |
URL url = new URL(actionUrl); |
HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
con.setRequestMethod( "POST" ); |
con.setRequestProperty( "Connection" , "Keep-Alive" ); |
con.setRequestProperty( "Charset" , "UTF-8" ); |
con.setRequestProperty( "Content-Type" , |
"multipart/form-data;boundary=" + boundary); |
DataOutputStream ds = new DataOutputStream(con.getOutputStream()); |
ds.writeBytes(twoHyphens + boundary + end); |
ds.writeBytes( "Content-Disposition: form-data; " |
+ "name=\"file1\";filename=\"" + newName + "\"" + end); |
FileInputStream fStream = new FileInputStream(uploadFile); |
byte [] buffer = new byte [bufferSize]; |
while ((length = fStream.read(buffer)) != - 1 ) { |
ds.write(buffer, 0 , length); |
ds.writeBytes(twoHyphens + boundary + twoHyphens + end); |
InputStream is = con.getInputStream(); |
StringBuffer b = new StringBuffer(); |
while ((ch = is.read()) != - 1 ) { |
showDialog( "上传成功" + b.toString().trim()); |
private void showDialog(String mess) { |
new AlertDialog.Builder(PhotoUpload. this ).setTitle( "Message" ) |
.setNegativeButton( "确定" , new DialogInterface.OnClickListener() { |
public void onClick(DialogInterface dialog, int which) { |
服务器端servlet
import java.io.IOException; |
import java.util.Iterator; |
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
import org.apache.commons.fileupload.DiskFileUpload; |
import org.apache.commons.fileupload.FileItem; |
public class UploadServlet extends HttpServlet { |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
throws ServletException, IOException { |
String temp = request.getSession().getServletContext().getRealPath( "/" ) |
System.out.println( "temp=" + temp); |
String loadpath = request.getSession().getServletContext() |
System.out.println( "loadpath=" + loadpath); |
DiskFileUpload fu = new DiskFileUpload(); |
fu.setSizeMax( 1 * 1024 * 1024 ); |
fu.setSizeThreshold( 4096 ); |
fu.setRepositoryPath(temp); |
fileItems = fu.parseRequest(request); |
System.out.println( "fileItems=" + fileItems); |
Iterator iter = fileItems.iterator(); |
FileItem item = (FileItem) iter.next(); |
if (!item.isFormField()) { |
String name = item.getName(); |
name = name.substring(name.lastIndexOf( "\\" ) + 1 ); |
long size = item.getSize(); |
if ((name == null || name.equals( "" )) && size == 0 ) |
int point = name.indexOf( "." ); |
name = ( new Date()).getTime() |
+ name.substring(point, name.length()) + index; |
File fNew = new File(loadpath, name); |
String fieldvalue = item.getString(); |
response.sendRedirect( "result.jsp?text1=" + text1); |
activity_main.xml
android:layout_width = "match_parent" |
android:layout_height = "match_parent" > |
android:id = "@+id/myText2" |
android:layout_width = "wrap_content" |
android:layout_height = "wrap_content" |
android:layout_alignParentTop = "true" |
android:layout_centerHorizontal = "true" |
android:layout_marginTop = "116dp" |
android:inputType = "textPersonName" /> |
android:id = "@+id/myText3" |
android:layout_width = "wrap_content" |
android:layout_height = "wrap_content" |
android:layout_alignLeft = "@+id/myText2" |
android:layout_below = "@+id/myText2" |
android:layout_marginTop = "45dp" |
android:id = "@+id/myButton" |
android:layout_width = "wrap_content" |
android:layout_height = "wrap_content" |
android:layout_below = "@+id/myText3" |
android:layout_centerHorizontal = "true" |
android:layout_marginTop = "61dp" |
以上是关于android 上传文件到服务器的主要内容,如果未能解决你的问题,请参考以下文章
Android客户端与Python服务器端通信之上传图片
android 上传文件到服务器
使用客户端证书和 Android 的 HttpsURLConnection 通过 SSL 上传文件
android中如何上传图片到FTP服务器
将文本文件从 Android 上传到 Rails 会导致内容类型欺骗错误
android批量文件上传(android批量图片上传)