基本信息
源码名称:jsp 单文件上传下载 例子源码
源码大小:6.05M
文件格式:.rar
开发语言:Java
更新时间:2015-08-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


package com.cc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.cc.bean.UserInfo;
import com.cc.dao.UserInfoDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class uploadAction extends ActionSupport {
	private int id;
	
	UserInfoDao uif =new UserInfoDao();
	private UserInfo userInfo;
	private List<UserInfo> userInfoList;

	private File myfile=null;
	private String myfileFileName="";
	private String myfileContentType="";
	
	
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public File getMyfile() {
		return myfile;
	}

	public void setMyfile(File myfile) {
		this.myfile = myfile;
	}

	public String getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(String myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

	public String getMyfileContentType() {
		return myfileContentType;
	}

	public void setMyfileContentType(String myfileContentType) {
		this.myfileContentType = myfileContentType;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	
	public List<UserInfo> getUserInfoList() {
		return userInfoList;
	}

	public void setUserInfoList(List<UserInfo> userInfoList) {
		this.userInfoList = userInfoList;
	}
	
	//上传文件并且新增用户信息
	public String doUpload() throws IOException, SQLException, ClassNotFoundException{
		String savePath=ServletActionContext.getServletContext().getRealPath("/upload/");
		if(myfile!=null){
			File saveFile =new File(new File(savePath),this.myfileFileName);
			if(!saveFile.getParentFile().exists())
			{
				saveFile.getParentFile().mkdirs();
				
			}
			FileUtils.copyFile(myfile, saveFile);
			ActionContext.getContext().put("message", "上传成功");
		}
		userInfo.setFilename(this.myfileFileName);
		uif.addUserInfo(userInfo);
		
		return "insertuser";
	}
	
	//查询用户信息
	public String selUserInfo() throws SQLException, ClassNotFoundException{
		this.userInfoList =uif.selUserInfo();
		return SUCCESS;
	}
	
	//查看图片
	public String selUser() throws SQLException, ClassNotFoundException{
		this.userInfo=uif.getUserInfo(this.id);
		return "seluser";
	}
	
	//下载文件
	public void downloadLocal() throws SQLException, ClassNotFoundException, FileNotFoundException{
		this.userInfo=uif.getUserInfo(this.id);
		String fileName=this.userInfo.getFilename();
		String path=ServletActionContext.getServletContext().getRealPath("/upload/");
		//读取到流中
		InputStream inStream =new FileInputStream(path "\\" fileName);
		//获得respon对象
		ActionContext context=ActionContext.getContext();
		HttpServletResponse response=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);
		//设置输出格式
		response.reset();
		response.setContentType("bin");
		response.addHeader("Content-Disposition", "attachment;filename=\"" fileName "\"");
		//循环取出流中的数据
		byte[] b=new byte[1024];
		int len;
		try {
			while((len=inStream.read(b))>0){
				response.getOutputStream().write(b,0,len);
			}
			inStream.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}