buf
=
new
byte[1024]
//read是输入流中的一个方法,判断流中是否还有数据,c!=-1就意味着流中无数据,所以应跳出循环,跟溢出没关系
while((c
=
fis.read(buf))!
=-1){
fos.write(buf,o,c)//读一字节写一字节
}
String realpath = ServletActionContext.getServletContext().getRealPath("/upload") //获取服务器路径String[] targetFileName = uploadFileName
for (int i = 0i <upload.lengthi++) {
File target = new File(realpath, targetFileName[i])
FileUtils.copyFile(upload[i], target)
//这是一个文件复制类copyFile()里面就是IO操作,如果你不用这个类也可以自己写一个IO复制文件的类
}
其中private File[] upload// 实际上传文件
private String[] uploadContentType// 文件的内容类型
private String[] uploadFileName// 上传文件名
这三个参数必须这样命名,因为文件上传控件默认是封装了这3个参数的,且在action里面他们应有get,set方法
public static int transFile(InputStream in, OutputStream out, int fileSize) {int receiveLen = 0
final int bufSize = 1000
try {
byte[] buf = new byte[bufSize]
int len = 0
while(fileSize - receiveLen >bufSize)
{
len = in.read(buf)
out.write(buf, 0, len)
out.flush()
receiveLen += len
System.out.println(len)
}
while(receiveLen <fileSize)
{
len = in.read(buf, 0, fileSize - receiveLen)
System.out.println(len)
out.write(buf, 0, len)
receiveLen += len
out.flush()
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace()
}
return receiveLen
}
这个方法从InputStream中读取内容,写到OutputStream中。
那么发送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至于存到数据库里嘛,Oracle里用Blob。搜索一下,也是一样的。从Blob能获取一个输出流。