关于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);
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值