是上传一个文件?
还是在form中填写的内容?
如果要将在页面上填写的form内容进行放到servlet处理到是很简单的。

解决方案 »

  1.   

    URLConnection中实现form
    要不直接在连接的后面带参数直接指向绝对路径!
    再由一个客户端程序进行处理形成流文件交给服务器端处理
      

  2.   

    URLConnection.getOutStream()按照Http格式把你的文件内容写进去,就没问题了
      

  3.   

    模拟上传的代码,请自行优化:import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.Socket;
    public class SimulateUpload
    {
        private static final byte[] BOUNDARY = "---------------------------7d33bc1b47038c".getBytes();
        private static final byte[] CRLF = "\r\n".getBytes();    public void simUpload(String page, String[] paramNames, Object[] paramValues, OutputStream os) throws IOException
        {
            int contentLength = calculateLength(paramNames, paramValues);        os.write(("POST " + page + " HTTP/1.1\r\n").getBytes());
            os.write("User-Agent: myselfHttp/1.0\r\n".getBytes());
            os.write("Accept: */*\r\n".getBytes());
            os.write("Host: localhost\r\n".getBytes());
            os.write("Content-Type: multipart/form-data; boundary=".getBytes());
            os.write(BOUNDARY);
            os.write(CRLF);
            os.write(("Content-Length: " + contentLength).getBytes());
            os.write(CRLF);
            os.write(CRLF);        os.write(BOUNDARY);
            os.write(CRLF);
            for(int i=0; i<paramNames.length; i++)
            {
                if(paramValues[i] instanceof String)
                    writeParameter(os, paramNames[i], (String) paramValues[i]);
                else if(paramValues[i] instanceof File)
                    writeFile(os, paramNames[i], (File) paramValues[i]);
            }
            os.flush();
        }    private void writeFile(OutputStream os, String paramName, File file) throws IOException
        {
            os.write("Content-Disposition: form-data; name=\"".getBytes());
            os.write(paramName.getBytes());
            os.write("\"; filename=\"".getBytes());
            os.write(file.getAbsolutePath().getBytes());
            os.write("\"\r\nContent-Type: application/octet-stream".getBytes());
            os.write(CRLF);
            os.write(CRLF);        FileInputStream fis = new FileInputStream(file);
            byte[] buf = new byte[4096];
            int bytes = 0;
            while((bytes = fis.read(buf)) != -1)
                os.write(buf, 0, bytes);
            fis.close();        os.write(CRLF);
            os.write(BOUNDARY);
            os.write(CRLF);
        }    private void writeParameter(OutputStream os, String name, String value) throws IOException
        {
            os.write("Content-Disposition: form-data; name=\"".getBytes());
            os.write(name.getBytes());
            os.write('"');
            os.write(CRLF);
            os.write(CRLF);
            os.write(value.getBytes());
            os.write(CRLF);
            os.write(BOUNDARY);
            os.write(CRLF);
        }    private int calculateLength(String[] paramNames, Object[] paramValues)
        {
            int result = BOUNDARY.length + CRLF.length;
            for(int i=0; i<paramNames.length; i++)
            {
                if(paramValues[i] instanceof String)
                    result += 88 + paramNames[i].getBytes().length + ((String)paramValues[i]).getBytes().length;
                else if(paramValues[i] instanceof File)
                {
                    File file = (File) paramValues[i];
                    result += 141 + paramNames[i].getBytes().length + file.getAbsolutePath().getBytes().length + file.length();
                }
            }
            return result;
        }    public static void main(String[] args)
        {
            try
            {
                Socket socket = new Socket("localhost", 80);
                OutputStream os = socket.getOutputStream();
                //OutputStream os = new FileOutputStream("request");
                SimulateUpload sim = new SimulateUpload();
                String[] paramNames = new String[]{"param1", "param2", "param3", "file", "file2"};
                Object[] paramValues = new Object[]{"value1", "value2", "value3", new File("d:\\test.txt"), new File("d:\\test1.txt")};
                sim.simUpload("/uploadServlet", paramNames, paramValues, os);
                os.flush();
                os.close();
                socket.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }