//资源浏览与下载类
//作者:赵宏
package com.windy.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResourceDownBean
{
private List resourceList;
private DataModel model;
private int rowIndex = -1;
private int totalResource=0; //资源总数
private int beginShow=0; //开始显示资源序号
private int numberPerPage=5; //每页显示资源数量
private List allResourceList;
private String selected="";
private String words="";
private SelectItem[] word;
private int currentPage;
private int totalPage;
public int getCurrentPage()
{
currentPage=beginShow/numberPerPage+1;
return currentPage;
}
public int getTotalPage()
{
totalPage=totalResource/numberPerPage;
if(totalResource % numberPerPage!=0) totalPage++;
return totalPage;
}
//清除已选定的关键词
public String reSelect()
{
words="";
return "refresh_page";
}
//按照关键词搜索资源
public String go() throws java.lang.Exception
{
beginShow=0;
totalResource=0;
pageGetResourceList(beginShow, words);
System.out.println("words="+words);
return "refresh_page";
}
//追加新选关键词到已选关键词组中,关键词之间以空格分隔
public void selectChange(ValueChangeEvent event)
{
String str=event.getNewValue().toString();
if(words.indexOf(str)==-1) words=words+" "+str;
}
//获取关键词列表
public SelectItem[] getWord() throws java.lang.Exception
{
if(word!=null) return word;
int total=0;
if(BaseClass.getStrConn()==null) BaseClass.class.newInstance();
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn=DriverManager.getConnection(BaseClass.getStrConn(),BaseClass.getNameConn(),BaseClass.getPassConn());
try
{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rset=stmt.executeQuery("select count(*) from keyworld_list");
if (rset.next()) total=Integer.parseInt(rset.getString(1));
if(word==null) word=new SelectItem[total+1];
rset=stmt.executeQuery("select word from keyworld_list order by word asc");
word[0]=new SelectItem(" "," ");
int k=1;
while(rset.next())
{
String str;
str=rset.getString(1);
word[k]=new SelectItem(str,str);
k++;
}
rset.close();
stmt.close();
}
catch(SQLException e)
{
throw new java.lang.Exception("Error executing query for loading projects",e);
}
conn.close();
return word;
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
//资源上翻页
public String prevPage() throws java.lang.Exception
{
if(beginShow>0) beginShow-=numberPerPage;
if(beginShow<0) beginShow=0;
pageGetResourceList(beginShow);
return "refresh_page";
}
//资源下翻页
public String nextPage() throws java.lang.Exception
{
if(beginShow+numberPerPage<totalResource)
{
beginShow += numberPerPage;
}
if(beginShow>=totalResource) beginShow -= numberPerPage;
pageGetResourceList(beginShow);
return "refresh_page";
}
//包装资源,便于用dataTable控件显示
public DataModel getResourceList() throws java.lang.Exception
{
if(model == null)
{
model = new ListDataModel();
model.setWrappedData(pageGetResourceList(beginShow));
}
return model;
}
//给当前页资源列表变量resourceList赋值
public List pageGetResourceList(int begin) throws java.lang.Exception
{
int zh;
if(totalResource==0) myGetResourceList();
if(resourceList==null) resourceList=new ArrayList();
resourceList.clear();
for(zh=begin;zh<begin+numberPerPage&&zh<totalResource;zh++)
resourceList.add(allResourceList.get(zh));
System.out.println("resourceList_size="+resourceList.size());
return resourceList;
}
//按关键词搜索资源
public List pageGetResourceList(int begin, String keyword) throws java.lang.Exception
{
int zh=0;
if(totalResource==0) myGetResourceList(keyword);
if(resourceList==null) resourceList=new ArrayList();
resourceList.clear();
System.out.println("totalResource="+totalResource+"begin="+begin);
System.out.println("totalResource="+totalResource+"allResourceList_size"+allResourceList.size());
for(zh=begin;zh<begin+numberPerPage&&zh<totalResource;zh++)
{
resourceList.add(allResourceList.get(zh));
System.out.println("totalResource="+totalResource+"zh="+zh);
}
System.out.println("resourceList_size="+resourceList.size());
return resourceList;
}
//获取当前选择的行编号
public int getSelectedRowIndex()
{
return rowIndex;
}
//资源下载
public String select() throws java.lang.Exception
{
rowIndex = model.getRowIndex()+beginShow;
ResourceBean rs=(ResourceBean)allResourceList.get(rowIndex);
String url=rs.getR_url();
String filename=rs.getR_name();
// System.out.println(rs.getR_url());
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request=(HttpServletRequest)ctx.getExternalContext().getRequest();
System.out.println("in download");
String filepath="z:\\resource\\"+url;
if(!new File(filepath).exists())
{
System.out.println("文件不存在...........");
return "no_success";
}
FacesContext facesContext = FacesContext.getCurrentInstance();
User myUser=(User)FacesContext.getCurrentInstance().getApplication().createValueBinding("#{sessionScope.user}").getValue(facesContext);
if(myUser==null)
{
return "no_login";
}
if(myUser.getU_name()=="guest")
{
return "guest_login";
}
if(myUser.getU_score()<=0)
{
return "guest_login";
}
try
{
String contentType = "application/octet-stream;charset=iso8859-1";
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
response.setContentType(contentType);
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
contentDisposition.append("filename=\"");
contentDisposition.append(filename);
contentDisposition.append("\"");
//logger.debug(System.getProperty("file.encoding"));
response.setHeader("Content-Disposition", new String(
contentDisposition.toString().getBytes(
System.getProperty("file.encoding")), "iso8859-1"));
//logger.debug(contentDisposition.toString());
OutputStream out = response.getOutputStream();
byte[] bytes = new byte[1024];
InputStream is = new FileInputStream(filepath);
int b = 0;
while ((b = is.read(bytes, 0, 1024)) > 0)
{
// System.out.println("b="+b);
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("exit download!");
UpdateResource(rs.getR_no(),rs.getR_cost(),myUser.getU_name());
return "success";
}
//资源下载成功后更新资源的属性,即下载次数加一,同时用户资源分减一
public void UpdateResource(int no,int cost,String username) throws java.lang.Exception
{
String sql;
if(BaseClass.getStrConn()==null) BaseClass.class.newInstance();
Class.forName("oracle.jdbc.driver.OracleDriver").newIns
JSF工程实例源代码
4星 · 超过85%的资源 需积分: 9 16 浏览量
2009-01-16
18:16:07
上传
评论
收藏 7.06MB RAR 举报
zhaoh2001sf
- 粉丝: 3
- 资源: 37
最新资源
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈