【原创随笔】jqUploader jsp开发事例

1,访问http://www.pixeline.be/experiments/jqUploader/下载jquery.jqUploader.1.0.2.3.zip,解压文件夹至项目中

其中的php文件及demo.html、style.css可删除,不会影响功能

2,将

<script src="js/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript" src="js/jquery.jqUploader.1.0.2.3/jquery.flash.js"></script>
<script type="text/javascript" src="js/jquery.jqUploader.1.0.2.3/jquery.jqUploader.js"></script>

导入到jsp文件中,配置jqUploader

$(document).ready(function() {
$('#example1').jqUploader({
debug:0      //debug模式,1为开启
,background:''  //背景色
,barColor:'FFDD00'//进度条颜色
,maxFileSize: '1048576'  //允许文件大小
,src:'js/jquery.jqUploader.1.0.2.3/jqUploader.swf'  //swf位置
,allowedExt:'*.avi; *.jpg; *.jpeg; *.png'       //允许格式
,allowedExtDescr: '*.avi; *.jpg; *.jpeg; *.png'    //选择窗格式描述
,validFileMessage: ''                   //验证文件的描述
,endMessage: 'and don\'t you come back ;)'       
,hideSubmit: false
,endMessage:'上传成功'                    //成功后的描述
,uploadScript:'upload!upload'               //提交的服务端action
});
});
<span id="example1">
<label for="example1_field" >
Choose a file to upload:
</label>
<input name="file" id="example1_field" type="file" />
</span>

   配置结果:

3,服务端如果没用struts2可自行获取,如果用struts2的朋友,因为struts2对文件表单会进行再次封装,所以用阿帕奇的文件获取方式是不行的,只能用struts2自己的文件获取方式来获取,即声明File对象及setter和getter,效果如下

    private File filedata;
private String filename;

public void setFiledata(File filedata) {
this.filedata = filedata;
}

public File getFiledata() {
return filedata;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String getFilename() {
return filename;
}

4,

public String upload() {
try {
String fileBasePath = ServletActionContext.getServletContext().getRealPath("/upload") + "\\complaint\\";
String newName = "";
if (filedata != null && filedata.length() > 0) {
String type = filename.substring(filename.lastIndexOf("."), filename.length());
newName = UUID.randomUUID().toString() + type;
File path = new File(fileBasePath);
if (!path.exists()) {
path.mkdirs();
}
path = null;
File outFile = new File(fileBasePath, newName);
if (!outFile.exists()) {
outFile.createNewFile();
}
FileInputStream in = new FileInputStream(filedata);
FileOutputStream out = new FileOutputStream(outFile);
byte[] bts = new byte[1024];
while ((in.read(bts)) != -1) {
out.write(bts);
}
in.close();
out.flush();
out.close();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}






posted @ 2012-01-18 14:56  为牛马者,背负前行  阅读(1900)  评论(0编辑  收藏  举报

如果本页面列出的内容侵犯了您的权益,请告知。
知识共享许可协议
996.icu