android使用开源项目 https://github.com/loopj/android-async-http 发送文件给后台Servlet
前台Android代码:
activity_main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上传"/> </LinearLayout>
import java.io.File; import org.apache.http.Header; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; public class MainActivity extends Activity { private EditText editText = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); } public void click(View view){ AsyncHttpClient client = new AsyncHttpClient(); String url = "http://192.168.120.1:8080/AndroidFileUpload/UpLoadServlet"; String path = editText.getText().toString().trim(); File file = new File(path); if(file.exists() && file.length() > 0){ RequestParams params = new RequestParams(); try { params.put("profile_picture", file); } catch (Exception e) { e.printStackTrace(); } client.post(url, params, new AsyncHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, "上传成功", 0).show(); } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(MainActivity.this, "上传失败", 0).show(); } }); }else { Toast.makeText(MainActivity.this, "文件不存在", 0).show(); } } }
import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class UpLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(isMultipart){ String realPath = this.getServletContext().getRealPath("upload"); System.out.println(realPath); File dir = new File(realPath); if(!dir.exists()){ dir.mkdir(); } DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("utf-8"); try { List<FileItem> items = upload.parseRequest(request); for(FileItem item : items){ if(item.isFormField()){ //username="username" String name = item.getFieldName(); String value = item.getString("utf-8"); System.out.println(name + " = " + value); } else { //文件 String name = item.getName(); item.write(new File(dir, System.currentTimeMillis() + name.substring(name.lastIndexOf(".")))); } } } catch (Exception e) { e.printStackTrace(); } } } }至于后台若使用Struts2接收文件,该怎样编写代码,还未测试成功......求指教