利用Smartupload工具包实现上传下载

1.02.jsp 前台页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!--  前台使用UTF-8编码,修改了Smartupload.jar包的源码,已经修改了它默认的GBK的编码变为UTF-8,防止后台乱码-->

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP '01.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>


 <%--   <form action="uploadServlet.do" method="post" enctype="multipart/form-data">
  请选择文件:<input id="myfile" name="myfile" type="file" οnchange="showPreview(this)"/>
  <input type="submit" value="提交"  />${result}
  </form> --%>
  <form action="SmartUploadServlet.do" method="post" enctype="multipart/form-data">
  上传文件1:<input type="file" name="myfile1">
  上传文件2:<input type="file" name="myfile2">
  上传文件3:<input type="file" name="myfile3">
  <input type="submit" value="提交"  />${result}
  </form>
  下载 <a href="SmartDownloadServlet.do?filename=高瑞的葵花宝典1.txt">高瑞的葵花宝典1.txt</a>
<%-- 下载:<a href="downloadServlet.do?filename=xxx.txt">xxx.txt</a> &nbsp;&nbsp; ${errorResult}
  --%>
  <div id="large"></div>
 
  </body>

</html>

2.SmartDownloadServlet  后台实现下载的Servlet

package com.imooc.servlet;


import java.io.IOException;
import java.io.PrintWriter;


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


import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;


public class SmartDownloadServlet extends HttpServlet {




public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);

}



public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = request.getParameter("filename");
filename=new String(filename.getBytes("ISO-8859-1"),"utf-8");//转码,防止乱码


System.out.println(filename);
SmartUpload su = new SmartUpload();


su.initialize(getServletConfig(), request, response);//初始化SmartUpload 对象
su.setContentDisposition(null);//设置默认下载打开方式为空,实现常规下载
try {

System.out.println(filename);
su.downloadFile(getServletContext().getRealPath("/") + "images/"+filename);//获取文件的绝对路径实现下载功能
System.out.println(getServletContext().getRealPath("/") + "images/"+filename);
} catch (SmartUploadException e) {

e.printStackTrace();
}
}


}

3.SmartUploadServlet  后台实现上传功能的Servlet

package com.imooc.servlet;


import java.io.File;
import java.io.IOException;
import java.sql.SQLException;


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


import com.jspsmart.upload.SmartUpload;


public class SmartUploadServlet extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {


doPost(req, resp);
}



public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String filepath = getServletContext().getRealPath("/")+"images";//获取上传文件位置的绝对路径
System.out.println(filepath);
File file =  new File(filepath);
if(!file.exists()){
file.mkdir();//判断文件夹是否存在,不存在则自己创建。
}
SmartUpload su = new SmartUpload();
//初始化SmartUpload对象
su.initialize(getServletConfig(), req, resp);
//设置上传文件的最大大小
su.setMaxFileSize(1024*1024*10);
//设置所有上传文件的最大大小
su.setTotalMaxFileSize(1024*1024*100);
//设置允许上传文件的类型
su.setAllowedFilesList("txt,jpg");
String result = "上传成功!";
try {
//设置不允许上传文件的类型
su.setDeniedFilesList("rar");
//上传文件
su.upload();
int count = su.save(filepath);
System.out.println("共上传了"+count+"个文件");
} catch (Exception e) {
result="上传失败!";

//异常相对应的字符,让用户更清晰
if(e.getMessage().indexOf("1015")!=-1){
result="上传失败:上传文件类型不正确";
}
else if(e.getMessage().indexOf("1010")!=-1){
result="上传失败:上传文件类型不正确";
}
else if(e.getMessage().indexOf("1105")!=-1){
result="上传失败:上传文件大小大于允许上传的最大值";
}
else if(e.getMessage().indexOf("1110")!=-1){
result="上传失败:上传文件大小大于允所有许上传的最大值";
}

e.printStackTrace();
}

//打印出上传文件的相应信息
for(int i =0 ; i<su.getFiles().getCount() ; i++){
com.jspsmart.upload.File tempfile = su.getFiles().getFile(i);
System.out.println("--------------------");
System.out.println("表单中name的值:"+tempfile.getFieldName());
System.out.println("上传文件名:"+tempfile.getFileName());
System.out.println("上传文件的大小:"+tempfile.getSize());
System.out.println("上传文件的拓展名:"+tempfile.getFileExt());
System.out.println("上传文件的全名:"+tempfile.getFilePathName());
System.out.println("--------------------");
}
req.setAttribute("result", result);
req.getRequestDispatcher("jsp/02.jsp").forward(req, resp);

}

项目整体路径

xml配置文件


}

http://download.csdn.net/detail/cartonwang/728517 SmartUpload.jar下载地址,修改了源码的,前台界面可以用UTF-8编码,后台稍微按上面的代码转一下都ok了

下一章应该是java线程或者是SpringMvc,Spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值