完整源码:http://yuncode.net/code/c_5063ece6eba2d94
客户端
001 | package com.example.testandroid; |
003 | import java.io.DataOutputStream; |
004 | import java.io.FileInputStream; |
005 | import java.io.InputStream; |
006 | import java.net.HttpURLConnection; |
009 | import android.app.Activity; |
010 | import android.app.AlertDialog; |
011 | import android.content.DialogInterface; |
012 | import android.os.Bundle; |
013 | import android.view.View; |
014 | import android.widget.Button; |
015 | import android.widget.TextView; |
017 | public class PhotoUpload extends Activity { |
018 | private String newName = "image.jpg" ; |
019 | private String uploadFile = "/sdcard/image.JPG" ; |
021 | private TextView mText1; |
022 | private TextView mText2; |
023 | private Button mButton; |
026 | public void onCreate(Bundle savedInstanceState) { |
027 | super .onCreate(savedInstanceState); |
028 | setContentView(R.layout.activity_main); |
029 | mText1 = (TextView) findViewById(R.id.myText2); |
031 | mText1.setText(uploadFile); |
032 | mText2 = (TextView) findViewById(R.id.myText3); |
034 | mText2.setText(actionUrl); |
036 | mButton = (Button) findViewById(R.id.myButton); |
037 | mButton.setOnClickListener( new View.OnClickListener() { |
038 | public void onClick(View v) { |
045 | private void uploadFile() { |
047 | String twoHyphens = "--" ; |
048 | String boundary = "*****" ; |
050 | URL url = new URL(actionUrl); |
051 | HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
053 | con.setDoInput( true ); |
054 | con.setDoOutput( true ); |
055 | con.setUseCaches( false ); |
058 | con.setRequestMethod( "POST" ); |
059 | con.setRequestProperty( "Connection" , "Keep-Alive" ); |
060 | con.setRequestProperty( "Charset" , "UTF-8" ); |
061 | con.setRequestProperty( "Content-Type" , |
062 | "multipart/form-data;boundary=" + boundary); |
064 | DataOutputStream ds = new DataOutputStream(con.getOutputStream()); |
065 | ds.writeBytes(twoHyphens + boundary + end); |
066 | ds.writeBytes( "Content-Disposition: form-data; " |
067 | + "name=\"file1\";filename=\"" + newName + "\"" + end); |
071 | FileInputStream fStream = new FileInputStream(uploadFile); |
073 | int bufferSize = 1024 ; |
074 | byte [] buffer = new byte [bufferSize]; |
077 | while ((length = fStream.read(buffer)) != - 1 ) { |
079 | ds.write(buffer, 0 , length); |
082 | ds.writeBytes(twoHyphens + boundary + twoHyphens + end); |
087 | InputStream is = con.getInputStream(); |
089 | StringBuffer b = new StringBuffer(); |
090 | while ((ch = is.read()) != - 1 ) { |
094 | showDialog( "上传成功" + b.toString().trim()); |
097 | } catch (Exception e) { |
098 | showDialog( "上传失败" + e); |
103 | private void showDialog(String mess) { |
104 | new AlertDialog.Builder(PhotoUpload. this ).setTitle( "Message" ) |
106 | .setNegativeButton( "确定" , new DialogInterface.OnClickListener() { |
107 | public void onClick(DialogInterface dialog, int which) { |
服务器端
04 | import java.io.IOException; |
06 | import java.util.Iterator; |
09 | import javax.servlet.ServletException; |
10 | import javax.servlet.http.HttpServlet; |
11 | import javax.servlet.http.HttpServletRequest; |
12 | import javax.servlet.http.HttpServletResponse; |
14 | import org.apache.commons.fileupload.DiskFileUpload; |
15 | import org.apache.commons.fileupload.FileItem; |
17 | public class UploadServlet extends HttpServlet { |
18 | public void doPost(HttpServletRequest request, HttpServletResponse response) |
19 | throws ServletException, IOException { |
21 | String temp = request.getSession().getServletContext().getRealPath( "/" ) |
23 | System.out.println( "temp=" + temp); |
24 | String loadpath = request.getSession().getServletContext() |
27 | System.out.println( "loadpath=" + loadpath); |
28 | DiskFileUpload fu = new DiskFileUpload(); |
30 | fu.setSizeMax( 1 * 1024 * 1024 ); |
31 | fu.setSizeThreshold( 4096 ); |
32 | fu.setRepositoryPath(temp); |
36 | List fileItems = null ; |
39 | fileItems = fu.parseRequest(request); |
40 | System.out.println( "fileItems=" + fileItems); |
41 | } catch (Exception e) { |
45 | Iterator iter = fileItems.iterator(); |
46 | while (iter.hasNext()) { |
47 | FileItem item = (FileItem) iter.next(); |
48 | if (!item.isFormField()) { |
49 | String name = item.getName(); |
50 | name = name.substring(name.lastIndexOf( "\\" ) + 1 ); |
51 | long size = item.getSize(); |
52 | if ((name == null || name.equals( "" )) && size == 0 ) |
54 | int point = name.indexOf( "." ); |
55 | name = ( new Date()).getTime() |
56 | + name.substring(point, name.length()) + index; |
58 | File fNew = new File(loadpath, name); |
61 | } catch (Exception e) { |
68 | String fieldvalue = item.getString(); |
75 | response.sendRedirect( "result.jsp?text1=" + text1); |