java中的文件上传(java文件上传功能)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享java中的文件上传的知识,其中也会对java文件上传功能进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
Java文件上传相关知识及得到后缀名
文件上传功能是最基本的 所以需要真正的掌握
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
! 用来提示信息
divspan 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/25990

java如何实现文件上传
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能获取一个输出流。
Java文件上传的几种方式
1、通过Servlet类上传
2、通过Struts框架实现上传
ITJOB学到两种方式
关于java中的文件上传和java文件上传功能的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
