文件上传功能是最基本的 所以需要真正的掌握
Java代码
=========文件上传功能================
@Property
private UploadedFile file
@Inject
private ApplicationGlobals globals
//获得绝对路径
String path = globals getServletContext() getRealPath( /images/person/head )
File copied = new File(path + / + file getFileName())
file write(copied) //写入项目
===========js=============
function fileBtn(){
var file=document getElementById( file ) value
//var houzui=kzName(file)
//获取文件后缀名并转成小写
var ext=file substring(file lastIndexOf( )) toLowerCase()
if( file value== ){
alert( 请选择上传的图片 )
return false
}else{
if(ext!= gif &&ext!= jpg &&ext!= jpeg &&ext!= bmp ) {
alert( 此图片类型不支持:[ +ext+ ] )
return false
}
}
return true
}
//获取后缀名
function kzName(u)
{
var s = / [^ ]+$/ exec(u)
return (s!=null)?s[ ]:null
}
=============file tml================
<div id= fileDiv >
<t:form>
图片上传
<input t:type= upload t:id= file name= file size= />
<input type= submit value= 提 交 onclick= return fileBtn()/>
</t:form>
<! 用来提示信息 >
<div><span id= errormsg ><t:if t:test= errorCode >${errorMsg}</t:if></span></div>
</div>
===============file java===相关代码=============
@Property
private UploadedFile file
@Persist(PersistenceConstants FLASH)
@Property
private String message
@Inject
private Messages messages
@Property
@Persist(value= flash )
private int errorCode
Object onUploadException(FileUploadException ex)
{
message = Upload exception: + ex getMessage()
return this
}
//用来在页面做提示信息
public String getErrorMsg(){
switch (errorCode) {
case :
return messages get( fileNameMsg )
case :
return messages get( fileSuccess )
default:
break
}
return
lishixinzhi/Article/program/Java/hx/201311/25990public 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能获取一个输出流。
Java代码实现文件上传
FormFile file=manform.getFile()String newfileName = null
String newpathname=null
String fileAddre="/numUp"
try {
InputStream stream = file.getInputStream()// 把文件读入
String filePath = request.getRealPath(fileAddre)//取系统当前路径
File file1 = new File(filePath)//添加了自动创建目录的功能
((File) file1).mkdir()
newfileName = System.currentTimeMillis()
+ file.getFileName().substring(
file.getFileName().lastIndexOf('.'))
ByteArrayOutputStream baos = new ByteArrayOutputStream()
OutputStream bos = new FileOutputStream(filePath + "/"
+ newfileName)
newpathname=filePath+"/"+newfileName
System.out.println(newpathname)
// 建立一个上传文件的输出流
System.out.println(filePath+"/"+file.getFileName())
int bytesRead = 0
byte[] buffer = new byte[8192]
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead)// 将文件写入服务器
}
bos.close()
stream.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)