急!急!在线候救!!怎么实现form上传 是上传一个文件?还是在form中填写的内容?如果要将在页面上填写的form内容进行放到servlet处理到是很简单的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 URLConnection中实现form要不直接在连接的后面带参数直接指向绝对路径!再由一个客户端程序进行处理形成流文件交给服务器端处理 URLConnection.getOutStream()按照Http格式把你的文件内容写进去,就没问题了 模拟上传的代码,请自行优化: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(); } }} 请问这个问题能否用到jdbc的事务回滚? 两个正则的区别 java学习哪方面好找工作啊 为什么String作为参数和基本数据类型一样用的值传递? 求救!一个Struts问题 运行中出现的问题,请帮忙解决呀! Java_home,path,classpath的关系? 求助!! 在linux下怎么设置jdk,path,classpath, Vector类方法使用的一个问题? 这样在Java调用Excel程序 java 如何适应win xp系统主题? jtable怎么更新???
要不直接在连接的后面带参数直接指向绝对路径!
再由一个客户端程序进行处理形成流文件交给服务器端处理
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();
}
}
}