服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用javabean实现文件上载(二)上载文件

上载文件

  众所周知,javabean是java平台的软件组件,下面要实现的上载功能就是用javabean实现,所以它可以方便地应用到任何需要文件上载功能的应用之中。

代码清单如下:

package com.brainysoftware.web;

import javax.servlet.http.httpservletrequest;
import javax.servlet.servletinputstream;
import java.util.dictionary;
import java.util.hashtable;
import java.io.printwriter;
import java.io.bufferedwriter;
import java.io.filewriter;
import java.io.ioexception;

public class fileuploadbean {

private string savepath, filepath, filename, contenttype;
private dictionary fields;

public string getfilename() {
return filename;
}

public string getfilepath() {
return filepath;
}

public void setsavepath(string savepath) {
this.savepath = savepath;
}

public string getcontenttype() {
return contenttype;
}

public string getfieldvalue(string fieldname) {
if (fields == null || fieldname == null)
return null;
return (string) fields.get(fieldname);
}

private void setfilename(string s) {
if (s==null)
return;

int pos = s.indexof("filename=/"");
if (pos != -1) {
filepath = s.substring(pos+10, s.length()-1);
// windows浏览器发送完整的文件路径和名字
// 但linux/unix和mac浏览器只发送文件名字
pos = filepath.lastindexof("//");
if (pos != -1)
filename = filepath.substring(pos + 1);
else
filename = filepath;
}
}
private void setcontenttype(string s) {
if (s==null)
return;

int pos = s.indexof(": ");
if (pos != -1)
contenttype = s.substring(pos+2, s.length());
}

public void doupload(httpservletrequest request) throws ioexception {
servletinputstream in = request.getinputstream();

byte[] line = new byte[128];
int i = in.readline(line, 0, 128);
if (i < 3)
return;
int boundarylength = i - 2;

string boundary = new string(line, 0, boundarylength); //-2丢弃换行字符
fields = new hashtable();

while (i != -1) {
string 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)
return;
//文件内容
i = in.readline(line, 0, 128);
setcontenttype(new string(line, 0, i-2));
i = in.readline(line, 0, 128);
//空行
i = in.readline(line, 0, 128);
newline = new string(line, 0, i);
printwriter pw = new printwriter(new bufferedwriter(new
filewriter((savepath==null? "" : savepath) + filename)));
while (i != -1 && !newline.startswith(boundary)) {
// 文件内容的最后一行包含换行字符
// 因此我们必须检查当前行是否是最
// 后一行
i = in.readline(line, 0, 128);
if ((i==boundarylength+2 || i==boundarylength+4)
&& (new string(line, 0, i).startswith(boundary)))
pw.print(newline.substring(0, newline.length()-2));
else
pw.print(newline);
newline = new string(line, 0, i);

}
pw.close();

}
else {
// 普通表单输入元素
// 获取输入元素名字
int pos = newline.indexof("name=/"");
string fieldname = newline.substring(pos+6, newline.length()-3);

i = in.readline(line, 0, 128);
i = in.readline(line, 0, 128);
newline = new string(line, 0, i);
stringbuffer fieldvalue = new stringbuffer(128);
while (i != -1 && !newline.startswith(boundary)) {
// 最后一行包含换行字符
// 因此我们必须检查当前行是否是最后一行
i = in.readline(line, 0, 128);
if ((i==boundarylength+2 || i==boundarylength+4)
&& (new string(line, 0, i).startswith(boundary)))
fieldvalue.append(newline.substring(0, newline.length()-2));
else
fieldvalue.append(newline);
newline = new string(line, 0, i);
}
fields.put(fieldname, fieldvalue.tostring());
}
}
i = in.readline(line, 0, 128);

}
}
}

  代码的第一行是包声明,如果你不想让该类从属于任何包,可以删除这行代码。接下来的几行代码声明了该javabean所要引用的各个类和接口。

  fileuploadbean类有5个私有的属性(域),6个公用的方法,2个私有的方法。

扫描关注微信公众号