当前位置: 技术问答>java相关
关于利用HTTP上载多个文件的问题(在线关注)
来源: 互联网 发布时间:2017-04-14
本文导语: 我做了好长时间没能搞定,请问一下怎样才能按照HTTP协议实现多个文件的上传,我现在对HTTP的文件头信息结构也不是很清楚,还望指教。 | while (i != -1){ newLine = new String(line, 0, i); ...
我做了好长时间没能搞定,请问一下怎样才能按照HTTP协议实现多个文件的上传,我现在对HTTP的文件头信息结构也不是很清楚,还望指教。
|
while (i != -1){
newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name=")){
if (newLine.indexOf("filename="") != -1){//文件
setFilename(new String(line, 0, i - 2));
if (filename != null &&!filename.trim().equals("")){
i = in.readLine(line, 0, BUFFER_SIZE); //content_type
setContentType(new String(line, 0, i - 2));
i = in.readLine(line, 0, BUFFER_SIZE); //空行
i = in.readLine(line, 0, BUFFER_SIZE); //file read begin
newLine = new String(line, 0, i);
fout = getFileOutputStream();
while (i != -1 && !newLine.startsWith(boundary)){
if ((i == boundaryLength + 2 || i == boundaryLength + 4)&&(new String(line, 0, i).startsWith(boundary)))
fout.write(line, 0, i - 2);
else
fout.write(line, 0, i);
i = in.readLine(line, 0, BUFFER_SIZE);
newLine = new String(line, 0, i);
}
fout.close();
}
}
else{ // 普通表单输入元素
int pos = newLine.indexOf("name="");
String fieldName = newLine.substring(pos + 6, newLine.length() - 3);
i = in.readLine(line, 0, BUFFER_SIZE);
i = in.readLine(line, 0, BUFFER_SIZE);
newLine = new String(line, 0, i);
StringBuffer fieldvalue = new StringBuffer(BUFFER_SIZE);
while (i != -1 && !newLine.startsWith(boundary)){
i = in.readLine(line, 0, BUFFER_SIZE);
if ((i == boundaryLength + 2 || i == boundaryLength + 4)&&(new String(line, 0, i).startsWith(boundary)))
fieldvalue.append(new String((newLine.substring(0, newLine.length() - 2)).getBytes("ISO8859-1"),"GB2312"));
else
fieldvalue.append(new String(newLine.getBytes("ISO8859-1"),"GB2312"));
newLine = new String(line, 0, i);
}
setFieldvalue(fieldName, fieldvalue.toString());
}
}
i = in.readLine(line, 0, BUFFER_SIZE);
}
protected FileOutputStream getFileOutputStream() throws IOException {
if(savePath == null || savePath.equals(""))
throw new IOException("保存目录设置错误:怎么不设置呢?");
File dir = new File(savePath);
if (dir.isFile()){
throw new IOException("保存目录设置错误:怎么是个文件呢?");
}
else if (!dir.exists()){
if (!dir.mkdirs()){
throw new IOException("保存目录设置错误:目录不存在,并且创建失败!!");
}
}
FileOutputStream fileOut = null;
Random rd = new Random();
int ranint = rd.nextInt(1000);
if (customFileName == null || customFileName.equals(""))
fileOut = new FileOutputStream(savePath + filename);
else{
customFileName += "_" + Integer.toString(ranint) + filepos;
fileOut = new FileOutputStream(savePath + customFileName);
}
return fileOut;
}
这段程序就支持多文件上传。其中有几个函数没贴上来,但不是主要的。
newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name=")){
if (newLine.indexOf("filename="") != -1){//文件
setFilename(new String(line, 0, i - 2));
if (filename != null &&!filename.trim().equals("")){
i = in.readLine(line, 0, BUFFER_SIZE); //content_type
setContentType(new String(line, 0, i - 2));
i = in.readLine(line, 0, BUFFER_SIZE); //空行
i = in.readLine(line, 0, BUFFER_SIZE); //file read begin
newLine = new String(line, 0, i);
fout = getFileOutputStream();
while (i != -1 && !newLine.startsWith(boundary)){
if ((i == boundaryLength + 2 || i == boundaryLength + 4)&&(new String(line, 0, i).startsWith(boundary)))
fout.write(line, 0, i - 2);
else
fout.write(line, 0, i);
i = in.readLine(line, 0, BUFFER_SIZE);
newLine = new String(line, 0, i);
}
fout.close();
}
}
else{ // 普通表单输入元素
int pos = newLine.indexOf("name="");
String fieldName = newLine.substring(pos + 6, newLine.length() - 3);
i = in.readLine(line, 0, BUFFER_SIZE);
i = in.readLine(line, 0, BUFFER_SIZE);
newLine = new String(line, 0, i);
StringBuffer fieldvalue = new StringBuffer(BUFFER_SIZE);
while (i != -1 && !newLine.startsWith(boundary)){
i = in.readLine(line, 0, BUFFER_SIZE);
if ((i == boundaryLength + 2 || i == boundaryLength + 4)&&(new String(line, 0, i).startsWith(boundary)))
fieldvalue.append(new String((newLine.substring(0, newLine.length() - 2)).getBytes("ISO8859-1"),"GB2312"));
else
fieldvalue.append(new String(newLine.getBytes("ISO8859-1"),"GB2312"));
newLine = new String(line, 0, i);
}
setFieldvalue(fieldName, fieldvalue.toString());
}
}
i = in.readLine(line, 0, BUFFER_SIZE);
}
protected FileOutputStream getFileOutputStream() throws IOException {
if(savePath == null || savePath.equals(""))
throw new IOException("保存目录设置错误:怎么不设置呢?");
File dir = new File(savePath);
if (dir.isFile()){
throw new IOException("保存目录设置错误:怎么是个文件呢?");
}
else if (!dir.exists()){
if (!dir.mkdirs()){
throw new IOException("保存目录设置错误:目录不存在,并且创建失败!!");
}
}
FileOutputStream fileOut = null;
Random rd = new Random();
int ranint = rd.nextInt(1000);
if (customFileName == null || customFileName.equals(""))
fileOut = new FileOutputStream(savePath + filename);
else{
customFileName += "_" + Integer.toString(ranint) + filepos;
fileOut = new FileOutputStream(savePath + customFileName);
}
return fileOut;
}
这段程序就支持多文件上传。其中有几个函数没贴上来,但不是主要的。