servletSmartUpload

package com.baihe.web.admin;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadFile extends HttpServlet
{

public DownloadFile()
{
}

public final void init(ServletConfig config)
throws ServletException
{
this.config = config;
}

public void destroy()
{
super.destroy();
}

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

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String url = request.getParameter("url");
int i = url.indexOf(".");
String path = (new StringBuilder(String.valueOf(request.getSession().getServletContext().getRealPath("/")))).append("wordToPdf\\").append(url.substring(0, i)).append(".pdf").toString();
SmartUpload su = new SmartUpload();
su.initialize(config, request, response);
su.setContentDisposition(null);
try
{
su.downloadFile(path);
}
catch(SmartUploadException e)
{
e.printStackTrace();
}
}

public void init()
throws ServletException
{
}

protected ServletConfig config;
}


在jsp中容易报异常,所以写到servlet里了,

(1)文件上传

Html代码
1 <%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.httpupload4j.SmartUpload"%><%@ page import="com.soft4j.bo.WebFileMgr"%><%
2 String ret = null;
3 SmartUpload su = null;
4 try{
5 su = new SmartUpload();
6 su.initialize(pageContext);
7 su.upload();
8 ret = WebFileMgr.upload(su,pageContext);//WebFileMgr.java 处理文件上传
9 if(ret!=null){
10 out.print("successed");
11 }
12 }catch(Exception e){
13 out.print("faild");
14 }finally{
15 su = null;
16 }
17 %>


WebFileMgr.upload(su,pageContext)方法内容:
Java代码
18 /**
19 * 上传方法.
20 * @param su
21 * @param pageContext
22 * @return
23 * @throws Exception
24 */
25 public static String upload(SmartUpload su,PageContext pageContext) throws Exception {
26 com.soft4j.httpupload4j.File suFile = null;
27 int fileCount = 0;
28 int maxFileSize = 900;//单个文件最大为900K
29 String AllowedExtensions=",jpg,jpeg,gif,png,";//允许上传的文件类型
30 try {
31
32 for (int i=0; i<su.getFiles().getCount();i++) {
33 suFile = su.getFiles().getFile(i);
34 if (suFile.isMissing())
35 continue;
36 fileSize = suFile.getSize()/1024;//字节转换成KB
37 if(fileSize==0) fileSize=1;
38
39 if(maxFileSize<fileSize) throw new Exception("单个上传文件的容量不能超过["+maxFileSize+"KB]");
40
41 if (suFile.getFileExt() == null
42 || "".equals(suFile.getFileExt())) {
43 fileExt = ",,";
44 } else {
45 fileExt = "," + suFile.getFileExt().toLowerCase() + ",";
46 }
47
48 if (!"".equals(AllowedExtensions)
49 && AllowedExtensions.indexOf(fileExt) == -1) {
50 throw new Exception("您上传的文件[" + suFile.getFileName()
51 + "]的类型为系统禁止上传的文件类型,不能上传!");
52 }
53
54 fileCount++;
55 }
56 if (fileCount==0) throw new Exception("请选择上传的文件");
57
58 StringBuffer fullFileName = null;//保存到服务器上的文件名(带路径)
59 for (int i=0; i<su.getFiles().getCount();i++) {
60 suFile = su.getFiles().getFile(i);
61 if (suFile.isMissing()) continue;
62 fullFileName = new StringBuffer("文件的路径");//填写 文件的路径
63 fullFileName.append("文件名");//填写 文件名
64 suFile.saveAs(fullFileName.toString(),SmartUpload.SAVE_PHYSICAL);
65 }
66 return "successed";
67 } finally {
68 //
69 }
70 }


上面的JSP,Java代码就可以实现文件的上传.

(2)文件下载

下载相对更为简单,只要设定下载的文件名(带路径)就可以了.

Html代码
71 <%@ page contentType="text/html;charset=UTF-8"%>
72 <%@ page import="com.soft4j.httpupload4j.SmartUpload"%>
73 <%
74 //新建一个SmartUpload对象
75 SmartUpload su = new SmartUpload();
76 //初始化
77 su.initialize(pageContext);
78 //设定要下载的文件名(带路径)
79 String fileName="下载的文件名";//文件名(带路径)
80 try{
81 su.downloadFile(fileName);//下载文件
82 }catch(Exception e){
83 e.printStackTrace();
84 out.println(e.toString());
85 }
86 response.getOutputStream().close();
87 %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值