需求:用HttpURLConnection模拟上传图片并把图片的名称也要传递过去.
简单分析:写入流的时候依次写入 图片名称 + "|" 分隔符 +  图片流
然后服务器接收的再处理流.分别取出图片名和图片.

 /** *//**
/** *//**
 * 上传方法
     * 上传方法
 * 返回上传完毕的文件名
     * 返回上传完毕的文件名
 * *
     * *
 */
     */
 public String upload(File f)
    public String upload(File f)

 
     {
{
 try
        try

 
         {
{
 //服务器IP(这里是从属性文件中读取出来的)
            //服务器IP(这里是从属性文件中读取出来的)
 String hostip = FileSupport.getServerIP();
            String hostip = FileSupport.getServerIP();
 URL url = new URL("http://"+ hostip +"/oxServer/ReceiveServlet");
            URL url = new URL("http://"+ hostip +"/oxServer/ReceiveServlet");
 
            
 HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
 //上传图片的一些参数设置
            //上传图片的一些参数设置
 uc
            uc
 .setRequestProperty(
                    .setRequestProperty(
 "Accept",
                            "Accept",
 "image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   application/x-shockwave-flash,   application/x-quickviewplus,   */*");
                            "image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   application/x-shockwave-flash,   application/x-quickviewplus,   */*");
 uc.setRequestProperty("Accept-Language", "zh-cn");
            uc.setRequestProperty("Accept-Language", "zh-cn");
 uc
            uc
 .setRequestProperty("Content-type",
                    .setRequestProperty("Content-type",
 "multipart/form-data;   boundary=---------------------------7d318fd100112");
                            "multipart/form-data;   boundary=---------------------------7d318fd100112");
 uc.setRequestProperty("Accept-Encoding", "gzip,   deflate");
            uc.setRequestProperty("Accept-Encoding", "gzip,   deflate");
 uc
            uc
 .setRequestProperty("User-Agent",
                    .setRequestProperty("User-Agent",
 "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1)");
                            "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1)");
 uc.setRequestProperty("Connection", "Keep-Alive");
            uc.setRequestProperty("Connection", "Keep-Alive");
 uc.setDoOutput(true);
            uc.setDoOutput(true);
 uc.setUseCaches(true);
            uc.setUseCaches(true);
 
        
 //读取文件流
            //读取文件流
 int size = (int) f.length();
            int size = (int) f.length();
 byte[] data = new byte[size];
            byte[] data = new byte[size];
 FileInputStream fis = new FileInputStream(f);
            FileInputStream fis = new FileInputStream(f);
 OutputStream out = uc.getOutputStream();
            OutputStream out = uc.getOutputStream();
 fis.read(data, 0, size);
            fis.read(data, 0, size);
 //写入文件名
            //写入文件名
 out.write(f.getName().trim().getBytes());
            out.write(f.getName().trim().getBytes());
 //写入分隔符
            //写入分隔符
 out.write('|');
            out.write('|');
 //写入图片流
            //写入图片流
 out.write(data);
            out.write(data);
 out.flush();
            out.flush();
 out.close();
            out.close();
 fis.close();
            fis.close();
 
            
 //读取响应数据
            //读取响应数据
 int code = uc.getResponseCode();
            int code = uc.getResponseCode();
 String sCurrentLine = "";
            String sCurrentLine = "";
 //存放响应结果
            //存放响应结果
 String sTotalString = "";
            String sTotalString = "";
 if (code == 200)
            if (code == 200)

 
             {
{
 java.io.InputStream is = uc.getInputStream();
                java.io.InputStream is = uc.getInputStream();
 BufferedReader reader = new BufferedReader(
                BufferedReader reader = new BufferedReader(
 new InputStreamReader(is));
                        new InputStreamReader(is));
 while ((sCurrentLine = reader.readLine()) != null)
                while ((sCurrentLine = reader.readLine()) != null)
 if (sCurrentLine.length() > 0)
                    if (sCurrentLine.length() > 0)
 sTotalString = sTotalString + sCurrentLine.trim();
                        sTotalString = sTotalString + sCurrentLine.trim();
 }
            }
 else
            else

 
             {
{
 sTotalString = "远程服务器连接失败,错误代码:" + code;
                sTotalString = "远程服务器连接失败,错误代码:" + code;
 }
            }
 return sTotalString;
            return sTotalString;
 }
        }
 catch (Exception e)
        catch (Exception e)

 
         {
{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
 return null;
        return null;
 }
    }
服务器Servlet:
 public void doGet(HttpServletRequest request, HttpServletResponse response)
public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException
            throws ServletException, IOException

 
     {
{
 ServletInputStream inStream = request.getInputStream(); // 取HTTP请求流
        ServletInputStream inStream = request.getInputStream(); // 取HTTP请求流
 int size = request.getContentLength(); // 取HTTP请求流长度
        int size = request.getContentLength(); // 取HTTP请求流长度

 byte[] buffer = new byte[size]; // 用于缓存每次读取的数据
        byte[] buffer = new byte[size]; // 用于缓存每次读取的数据
 byte[] result = new byte[size]; // 用于存放结果的数组
        byte[] result = new byte[size]; // 用于存放结果的数组
 int count = 0;
        int count = 0;
 int rbyte = 0;
        int rbyte = 0;
 // 循环读取
        // 循环读取
 while (count < size)
        while (count < size)

 
         {
{
 rbyte = inStream.read(buffer); // 每次实际读取长度存于rbyte中 sflj
            rbyte = inStream.read(buffer); // 每次实际读取长度存于rbyte中 sflj
 for (int i = 0; i < rbyte; i++)
            for (int i = 0; i < rbyte; i++)

 
             {
{
 result[count + i] = buffer[i];
                result[count + i] = buffer[i];
 }
            }
 count += rbyte;
            count += rbyte;
 }
        }

 // 先找到文件名和图片流的标志位'|'
        // 先找到文件名和图片流的标志位'|'
 int index = 0;
        int index = 0;
 for (int i = 0; i < result.length; i++)
        for (int i = 0; i < result.length; i++)

 
         {
{
 byte b = result[i];
            byte b = result[i];
 if (b == '|')
            if (b == '|')

 
             {
{
 index = i;
                index = i;
 break;
                break;
 }
            }
 }
        }

 // 存放文件名
        // 存放文件名
 byte name[] = new byte[index + 1];
        byte name[] = new byte[index + 1];
 // 存放图片字节
        // 存放图片字节
 byte[] img = new byte[size - index];
        byte[] img = new byte[size - index];
 for (int i = 0; i < result.length; i++)
        for (int i = 0; i < result.length; i++)

 
         {
{
 if (i < index)
            if (i < index)

 
             {
{
 name[i] = result[i];
                name[i] = result[i];
 }
            }
 if (i > index)
            if (i > index)

 
             {
{
 // 这时注意img数组的index要从0开始
                // 这时注意img数组的index要从0开始
 img[i - index - 1] = result[i];
                img[i - index - 1] = result[i];
 }
            }
 }
        }
 // 还原文件名
        // 还原文件名
 String fileName = new String(name);
        String fileName = new String(name);

 inStream.close();
        inStream.close();

 String newFileName = renameFile(fileName);
        String newFileName = renameFile(fileName);
 // 响应客户端
        // 响应客户端
 response.setContentType("text/html");
        response.setContentType("text/html");
 // 注意响应中文数据时要设置
        // 注意响应中文数据时要设置
 response.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
 PrintWriter out = response.getWriter();
        PrintWriter out = response.getWriter();
 //可能情况 0 数据库无相关记录 1 文件名不符合要求 其它情况为正常
        //可能情况 0 数据库无相关记录 1 文件名不符合要求 其它情况为正常
 if(newFileName.equals("0"))
        if(newFileName.equals("0"))

 
         {
{
 out.write("0|" + fileName);
            out.write("0|" + fileName);
 }
        }
 else if(newFileName.equals("1"))
        else if(newFileName.equals("1"))

 
         {
{
 out.write("1|" + fileName);
            out.write("1|" + fileName);
 }
        }
 else
        else

 
         {
{
 out.write(fileName);
            out.write(fileName);
 }
        }
 out.close();
        out.close();
 //上传错误中止后续操作
        //上传错误中止后续操作
 if(newFileName.length()<= 1)
        if(newFileName.length()<= 1)

 
         {
{
 return;
            return;
 }
        }
 
        
 File f = new File(ImageSupport.getOriginal() + "/" + newFileName);
        File f = new File(ImageSupport.getOriginal() + "/" + newFileName);
 FileOutputStream fos = new FileOutputStream(f);
        FileOutputStream fos = new FileOutputStream(f);
 fos.write(img);
        fos.write(img);
 fos.flush();
        fos.flush();
 fos.close();
        fos.close();
 // 改变图片大小后重新放置到新地点
        // 改变图片大小后重新放置到新地点
 ImageSupport.changeImageSize(f, ImageSupport.getAfter() + "/"
        ImageSupport.changeImageSize(f, ImageSupport.getAfter() + "/"
 + newFileName, 300, 300);
                + newFileName, 300, 300);
 }
    }
我写的是一个批量上传图片的程序,经测试通过.