jimingminlovefly

统计

最新评论

案例-jspSmartUpload上传下载1

<FORM METHOD="POST" ENCTYPE="multipart/form-data" 
ACTION="/jspSmartUpload/upload.jsp">
<INPUT TYPE="FILE" NAME="MYFILE">
<INPUT TYPE="SUBMIT">
</FORM>
 
上传+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<%
	// 新建一个SmartUpload对象
	SmartUpload su = new SmartUpload();
	// 上传初始化
	su.initialize(pageContext);
	// 设定上传限制
	// 1.限制每个上传文件的最大长度。
	// su.setMaxFileSize(10000);
	// 2.限制总上传数据的长度。
	// su.setTotalMaxFileSize(20000);
	// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
	// su.setAllowedFilesList("doc,txt");
	// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
	jsp,htm,html扩展名的文件和没有扩展名的文件。
	// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
	// 上传文件
	su.upload();
	// 将上传文件全部保存到指定目录
	int count = su.save("/upload");
	out.println(count+"个文件上传成功!<br>");
	
	// 利用Request对象获取参数之值
	out.println("TEST="+su.getRequest().getParameter("TEST")
	+"<BR><BR>");

	// 逐一提取上传文件信息,同时可保存文件。
	for (int i=0;i<su.getFiles().getCount();i++)
	{
		com.jspsmart.upload.File file = su.getFiles().getFile(i);
		
		// 若文件不存在则继续
		if (file.isMissing()) continue;

		// 显示当前文件信息
		out.println("<TABLE BORDER=1>");
		out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
		+ file.getFieldName() + "</TD></TR>");
		out.println("<TR><TD>文件长度(Size)</TD><TD>" + 
		file.getSize() + "</TD></TR>");
		out.println("<TR><TD>文件名(FileName)</TD><TD>" 
		+ file.getFileName() + "</TD></TR>");
		out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>" 
		+ file.getFileExt() + "</TD></TR>");
		out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
		+ file.getFilePathName() + "</TD></TR>");
		out.println("</TABLE><BR>");

		// 将文件另存
		// file.saveAs("/upload/" + myFile.getFileName());
		// 另存到以WEB应用程序的根目录为文件根目录的目录下
		// file.saveAs("/upload/" + myFile.getFileName(), 
		su.SAVE_VIRTUAL);
		// 另存到操作系统的根目录为文件根目录的目录下
		// file.saveAs("c:\\temp\\" + myFile.getFileName(), 
		su.SAVE_PHYSICAL);

	}
%>
下载+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
<%@ page contentType="text/html;charset=gb2312" 
import="com.jspsmart.upload.*" %><%
		// 新建一个SmartUpload对象
	SmartUpload su = new SmartUpload();
		// 初始化
	su.initialize(pageContext);
		// 设定contentDisposition为null以禁止浏览器自动打开文件,
		//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
		//doc时,浏览器将自动用word打开它。扩展名为pdf时,
		//浏览器将用acrobat打开。
	su.setContentDisposition(null);
		// 下载文件
	su.downloadFile("/upload/如何赚取我的第一桶金.doc");
%>
 
下载 中文处理+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void downloadFile(String s, String s1, String s2, int i)
	throws ServletException, IOException, SmartUploadException
    {
	if(s == null)
	    throw new IllegalArgumentException("File '" + s +
	    "' not found (1040).");
	if(s.equals(""))
	    throw new IllegalArgumentException("File '" + s +
	    "' not found (1040).");
	if(!isVirtual(s) && m_denyPhysicalPath)
	    throw new SecurityException("Physical path is
	    denied (1035).");
	if(isVirtual(s))
	    s = m_application.getRealPath(s);
	java.io.File file = new java.io.File(s);
	FileInputStream fileinputstream = new FileInputStream(file);
	long l = file.length();
	boolean flag = false;
	int k = 0;
	byte abyte0[] = new byte[i];
	if(s1 == null)
	    m_response.setContentType("application/x-msdownload");
	else
	if(s1.length() == 0)
	    m_response.setContentType("application/x-msdownload");
	else
	    m_response.setContentType(s1);
	m_response.setContentLength((int)l);
	m_contentDisposition = m_contentDisposition != null ?
	m_contentDisposition : "attachment;";
	if(s2 == null)
	    m_response.setHeader("Content-Disposition", 
	    m_contentDisposition + " filename=" + 
	    toUtf8String(getFileName(s)));
	else
	if(s2.length() == 0)
	    m_response.setHeader("Content-Disposition", 
	    m_contentDisposition);
	else
	    m_response.setHeader("Content-Disposition", 
	    m_contentDisposition + " filename=" + toUtf8String(s2));
	while((long)k < l)
	{
	    int j = fileinputstream.read(abyte0, 0, i);
	    k += j;
	    m_response.getOutputStream().write(abyte0, 0, j);
	}
	fileinputstream.close();
    }

    /**
     * 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
     * 纵横软件制作中心雨亦奇2003.08.01
     * @param s 原文件名
     * @return 重新编码后的文件名
     */
    public static String toUtf8String(String s) {
	StringBuffer sb = new StringBuffer();
	for (int i=0;i<s.length();i++) {
	    char c = s.charAt(i);
	    if (c >= 0 && c <= 255) {
		sb.append(c);
	    } else {
		byte[] b;
		try {
		    b = Character.toString(c).getBytes("utf-8");
		} catch (Exception ex) {
		    System.out.println(ex);
		    b = new byte[0];
		}
		for (int j = 0; j < b.length; j++) {
		    int k = b[j];
		    if (k < 0) k += 256;
		    sb.append("%" + Integer.toHexString(k).
		    toUpperCase());
		}
	    }
	}
	return sb.toString();
    }
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 

posted on 2011-11-04 17:31 计明敏 阅读(343) 评论(0)  编辑  收藏 所属分类: jsp/servlet


只有注册用户登录后才能发表评论。


网站导航: