⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadaction.java

📁 自己编写的一个spring框架下实现文件上传的例子
💻 JAVA
字号:
package com.mr.UpLoad;

import java.io.BufferedOutputStream;
import java.io.IOException;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

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


import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class DownloadAction implements Controller{
	private UpLoadDao upLoadDao;
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String id=request.getParameter("id");
		System.out.println("aaaaaaaaaaa"+id);
		//FileManager manager=new FileManager();
		//List<UploadFile> files=upLoadDao.queryObject("select * from tab_image where id="+id);
		//if(files!=null&&files.size()>0&&!files.isEmpty())
		//System.out.println("cccccccc");
		//else
			//System.out.println("ddddddddddd");
		//byte[] contents=file.getContents();
		//UploadFile file=(UploadFile)files.get(0);
        //System.out.println("ddddddddddd"+contents.length);
		final LobHandler lobHandler = new DefaultLobHandler();

		
		final UploadFile file = new UploadFile();
		upLoadDao.getObject("select * from tab_image where id=?", new Object[]{new Integer(id)}, new AbstractLobStreamingResultSetExtractor() {

		     protected void streamData(ResultSet rs) 

		            throws SQLException, IOException, DataAccessException {
		    	 file.setId(rs.getInt("id"));
		    	 file.setTpname(rs.getString("tpname"));
		    	 file.setPath(rs.getString("path"));
		    	 file.setTpsize(rs.getString("tpsize"));
		    	 file.setContents(lobHandler.getBlobAsBytes(rs, 5));

		     }

		  
		});

		try
	        {
	            response.setContentType("application/x-msdownload");
	            response.setHeader("Content-Disposition",
	                               "attachment;" + " filename="+ new String(file.getTpname().getBytes(), "ISO-8859-1"));
	           
	            BufferedOutputStream os=new BufferedOutputStream(response.getOutputStream());
	            
	          
	            os.write(file.getContents());
	            os.flush();
	            
	            os.close();
	            
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace();
	        }
		return null;
	}
	public UpLoadDao getUpLoadDao() {
		return upLoadDao;
	}
	public void setUpLoadDao(UpLoadDao upLoadDao) {
		this.upLoadDao = upLoadDao;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -