ssh download file

上传:

package com.wb.ekeng.web.action.file;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.wb.ekeng.ebo.BO_File;
import com.wb.ekeng.info.INFO_Admin;
import com.wb.ekeng.info.INFO_File;
import com.wb.ekeng.web.action.BaseAction;
import com.wb.ekeng.web.filemanage.File;
import com.wb.ekeng.web.filemanage.SmartUpload;
import com.wb.ekeng.web.util.Convertor;

public class ACT_AddFile extends BaseAction {

public ACT_AddFile() {
super();
}

public ActionForward doExcute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMessages errors) throws Exception {
//文件保存路径
String strSavePath ="/upload/file/";
//允许上传的文件类型
String strAllowExt ="jpg,jpeg,gif,bmp,txt,java";
//允许上传最大字节数
int maxSize =1048576;
//上传文件
SmartUpload upload=new SmartUpload();
upload.initialize(this.getServlet().getServletConfig(), request, response);
upload.upload("UTF-8");
//获取文件
String[] allowExtList =strAllowExt.split(",");
File fileBuffer =upload.getFiles().getFile(0);
if(fileBuffer.isMissing()){
throw new Exception("error.act.act_addfile.error3");
}

HttpSession session=request.getSession();
INFO_Admin loginAdmin=(INFO_Admin) session.getAttribute("loginadmin");
Long lAdminId=loginAdmin.getId();

String strName=upload.getRequest().getParameter("name");
//如果遇见上传中文文件出现乱码问题,上一句可以改成这样(其他语句参照这): String strName=new String(upload.getRequest().getParameter("name").getBytes(),"UTF-8");
String strNeedPoint=upload.getRequest().getParameter("needpoint");
String strType=upload.getRequest().getParameter("type");
String strDes=upload.getRequest().getParameter("des");

Integer iNeedPoint=null;

//验证参数
if(strName==null||strNeedPoint==null||strType==null||strDes==null){
throw new Exception("error.common.badrequest");
}
this.doValidate(errors,INFO_File.validateName(strName));
this.doValidate(errors,INFO_File.validateNeedPoint(strNeedPoint));
this.doValidate(errors,INFO_File.validateType(strType));
this.doValidate(errors,INFO_File.validateDes(strDes));

//验证文件大小
int FileSize=fileBuffer.getSize();
if(FileSize>maxSize){
this.doValidate(errors,"error.act.act_addfile.error2");
}

//验证文件类型
String strFileExt =fileBuffer.getFileExt();
boolean flag =false;
for(int i=0;i<allowExtList.length;i++) {
if(allowExtList[i].toLowerCase().equals(strFileExt.toLowerCase())){
//找到了匹配的后缀
flag=true;
}
}
if(strFileExt.equals("") || flag ==false){
this.doValidate(errors,"error.act.act_addfile.error1");
}

if(!errors.isEmpty()){
System.out.println(errors.toString());
return null;
}
//参数转换
strName =Convertor.convertHalfToFull(strName);
iNeedPoint=new Integer(strNeedPoint);
Integer iFileSize=Integer.valueOf(FileSize);

//构造saveName
String strSaveName=lAdminId+"_"+BO_File.getNowString()+"."+upload.getFiles().getFile(0).getFileExt();
fileBuffer.saveAs(strSavePath + strSaveName);

//提交数据
BO_File boFile=new BO_File();
boFile.addFile(strName,strType,strDes,strSaveName,iNeedPoint,lAdminId,iFileSize);
return new ActionForward("/admin/main/download/admindownload.do",true);
}
}

下载:
package com.wb.ekeng.web.action.file;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.wb.ekeng.web.action.BaseAction;
import com.wb.ekeng.ebo.BO_File;
import com.wb.ekeng.info.INFO_File;
import com.wb.ekeng.info.INFO_User;

import com.wb.ekeng.web.filemanage.SmartUpload;

/**
* 备注:
* 文件下载Action
* 输入:
* String fileid
* String userid
* 输出:
*/
public class ACT_Download extends BaseAction {

public ACT_Download() {
super();
}
public ActionForward doExcute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMessages errors) throws Exception {
HttpSession session =request.getSession();
//获取参数
Long lFileId=null;
String fileId =request.getParameter("fileid");
INFO_User infoUser=(INFO_User)session.getAttribute("loginuser");

//验证参数
if(fileId==null || infoUser.getId() ==null){
throw new Exception("error.common.badrequest");
}
lFileId=new Long(fileId);
BO_File boFile=new BO_File();
INFO_File infoFile =null;

//判断用户是否为第一次下载
接收存储下载资源Id的session,然后判断要下载的资源Id是否没存在于当前登录的下载页表中(即是否是第一次下载),如果是则调用下载扣除积分方法,并把这个资源的Id存入列表中。如果不是,则把标记位置false,直接下载资源,不调用扣除积分的方法。
boolean isFirstDownLoad =true;
ArrayList downFileList =(ArrayList)session.getAttribute("downfilelist");
for(int index =0; index <downFileList.size(); index ++){
Long lFileIdBuffer =(Long)downFileList.get(index);
if(lFileIdBuffer.longValue() ==lFileId.longValue()){
isFirstDownLoad =false;
break;
}
}
if(isFirstDownLoad){
infoFile=boFile.download(lFileId,infoUser.getId());
downFileList.add(lFileId);
}else{
infoFile=boFile.getFile(lFileId);
}
(下面就是有关下载的代码)
//新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
//初始化
su.initialize(this.getServlet().getServletConfig(), request, response);
//设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开。
su.setContentDisposition(null);
//下载文件
su.downloadFile("/upload/file/"+infoFile.getSaveName());
return mapping.findForward("success");
}}
空调铜管厂家 相册私密照片在哪里看 cadworx2015 pid案例 linux 多版本jdk 画家黄一中 verinurad na2s2o3与i2 hcl 幼儿园关于春天的歌曲 pgd 934 magnet 5d3拍摄视频快门速度 日本中医汉方书籍 光蓓净 gdp增长 房地产 能看卫星云图的app 超好听的轻音乐 宁波刘苏苏漫画91视频 适合给男朋友讲小黄文 for the past month he 宝马x1发动机 6sp换6p的电池好吗 恐怖老虎图片 honeywell 3800g设置 wds有线上不了 国家电网上班时间 ninjawrapper error 1 nhdta958磁力链接 president of france revit创建族异形柱 processing 鼠标qq enbd5015 磁力

Copyright 知识库 Some Rights Reserved

如反馈或投诉等情况联系:une35498#163.com