applications 传文件应该是socket编程
解决方案 »
- tomcat 启动报错
- ssh框架做简单项目,但是简单的查询总是出错啊!!
- apache重写jsp路径为静态路径
- Javascript杂谈
- 一道面试题引出的思考--欢迎大家都谈谈看法
- Invalid operation for the current cursor position.是什么错误?
- 用什么格式把变量和字符串赋给一个值
- 一个简单的输出流问题,输出到当前文件夹,在线等.........
- JSP中登陆Exchange邮箱的代码?(包括用户名、密码和域名参数)
- JSP数据连接池怎么实现?
- (高手请进)tomcat中,server.xml中的如下的参数如何访问?
- 更新数据出现错误,请高手帮忙(解决马上结贴)-------------------在线急等
content-type = multipart/form-data; boundary=---------------------------7d32c227
3202ce和
dataHeader::Content-Disposition: form-data; name="FILE4"; filename="C:\Documents
and Settings\fangdh\桌面\request.bak"
不过好象不行的。
看RFC1867后还是不是很明白的,不知道那个流到底是怎么处理。谢谢帮助!!!
我在JAVA中文站发表的文章,主要是分析了HTTP文件上传的格式和内容,希望对你有帮助
我只有自己写代码去读文件了。
好象进不去的
以下格式适合于FORM的ENCTYPE为multipart/form-data"的情况
其实在一个项目中,文件上传是很重要的一环,我们经常会用到网上下栽的现成的上传包,其实有时用在项目中感觉很别扭。如果自己编写一个,肯定会更符合项目的实际情况,比如在里面加上权限控制、文件按类型自动分开存放等等,其实只要搞清了HTTP的格式,编写这样的程序是不难的
下面是一个我测试用的例子,我把HTTP的内容打印了出来,如下:
-----------------------------7d333a81402d8
Content-Disposition: form-data; name="jitfile"; filename="F:\库存补充21230.txt"
Content-Type: text/plain 领料/退料单的来源单据加上服务单。
-----------------------------7d333a81402d8
Content-Disposition: form-data; name="mrpfile"; filename=""
Content-Type: application/octet-stream
-----------------------------7d333a81402d8--
再分析一下:
分隔符 //这个是随机的,每次都不一样
13 10 //这两个字符分割符拉,程序中通过判断这两个符号来决定读取到了那一部分
Content-Disposition:;name-"";filename="";//这些偶就不想说了
13 10
Content-Type:text/plain
13 10
13 10
文件内容
13 10
分隔符
45 45 13 10//这四个字符一起出现就表示了HTTP内容的结束
还有个注意的地方,就是在最开始从REQUEST中读取内容到数组的时候
是不能一次性读取完了,应该设个循环,其他地方小心点就是了
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
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").getBytes());
os.write(CRLF); os.write("User-Agent: myselfHttp/1.0".getBytes());
os.write(CRLF); os.write("Accept: */*".getBytes());
os.write(CRLF); os.write("Host: ".getBytes());
os.write(InetAddress.getLocalHost().getHostAddress().getBytes());
os.write(CRLF); 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();
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();
}
}
}