日期:2014-05-16  浏览次数:20338 次

关于java上传,使用jspsmart来实现上传,下载
1、好像只能写在servlet中,在其他地方的话,初始化有点问题不知道为什么总是获取不到文件。

2、上传代码如下:

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jspsmart.upload.SmartUpload;

/**
 * 
 * @author 赵永恩
 *
 */ 
public class Up extends HttpServlet {
	
	/**
	 * 
	 * service
	 * 
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setHeader("Pragma","No-cache");
		response.setContentType("text/html");
		HttpSession session = request.getSession();

		ServletContext context = session.getServletContext();
		String path = context.getRealPath("/");
		String file_configpath=path+"upfile/";
		File dir = new File(file_configpath);// 构建上传目录
			if (!dir.exists()) { 
				dir.mkdirs();   
			}
			try {
				// 新建一个SmartUpload对象
				SmartUpload su = new SmartUpload();
				//设置编码
				su.setCharset("UTF-8");
				// 上传初始化
				su.initialize(this.getServletConfig(),request, response);
				// 限制每个上传文件的最大长度。
				su.setMaxFileSize(500*1024*1024);
				// 限制总上传数据的长度。
				su.setTotalMaxFileSize(90000000);
				// 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
				su.setAllowedFilesList("rmvb,3pg,flv,mp4,wmv,wmv9");
				// 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
				su.setDeniedFilesList("exe,bat,jsp,htm,html");
				su.upload();
				
				//获取jsp页面的值
				String title =su.getRequest().getParameter("title");
				String msg =su.getRequest().getParameter("msg");

				for (int i = 0; i < su.getFiles().getCount(); i++) {
					com.jspsmart.upload.SmartFile file = su.getFiles().getFile(i);
					if (file.isMissing()) {
						//表示没有附加上传,针对页面有多个file的时候其中某个没有选择的情况
						continue;
					}
					String zykFileExt=file.getFileExt();
					String upname="2011_06_23_1103_0001."+zykFileExt;
					int zyksize=file.getSize()/(1024*1024);
					file.saveAs(file_configpath+upname);
				}
				System.out.println("上传文件,成功!");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("上传失败!请仔细检查。");
				e.printStackTrace();
			}
	}
}


3、下载代码如下:
<a href="download.jsp?upname=<%=upname%>">下载</a>


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import = "com.jspsmart.upload.*"%>
<%
ServletContext context = session.getServletContext();
String path = context.getRealPath("/");

String upname=request.getParameter("upname");
String file_configpath=path+"upfile/";

response.reset();
SmartUpload su = new SmartUpload();
response.reset();
su.initialize(pageContext);
su.setContentDisposition(null);
//添加下边两句是为了防止报错,之前是成功下载,但是后台报错
out.clear();
out = pageContext.pushBody();

su.downloadFile(file_configpath+upname);
%>