主要介绍使用 smartupload.jar 包中的方法对文件的上传和下载。上传时文件是存放在服务器中,我用的是tamcat。

首先建立一个servlet 类,对文件的操作

 package com.dkt.servlet;

 import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.lxh.smart.Files;
import org.lxh.smart.SmartUpload;
import org.lxh.smart.SmartUploadException; public class FileUpdownLoad 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 { response.setContentType("text/html");
request.setCharacterEncoding("GB2312");
response.setCharacterEncoding("GB2312");
SmartUpload su = new SmartUpload(); String op = request.getParameter("op");
if (("upload").equals(op)) {
su.initialize(getServletConfig(), request, response);//初始化
su.setAllowedFilesList("txt,jpg,png,docx,xml,PNG,xlsx");//允许上传的文件类型,区分大小写
try {
su.setDeniedFilesList("html,exe");//不允许上传的文件类型
} catch (SQLException e) {
e.printStackTrace();
}
su.setMaxFileSize(5000000);//文件大小 /*Files files = su.getFiles();
org.lxh.smart.File file2 = files.getFile(0);
System.out.println(file2.getFileName());
for(int i=0;i<files.getCount();i++){
org.lxh.smart.File file = files.getFile(i);
String fileName = file.getFileName();
System.out.println("fileName---->"+fileName);
fileName = new String(fileName.getBytes("ISO-8859-1"),"GB2312");
try {
file.saveAs(fileName);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}*/
try {
su.upload();//上传
su.save("/smartUpdownload");//上传的路径,在tomcat下的文件夹中
System.out.println("上传成功!!!");
} catch (SmartUploadException e) {
e.printStackTrace();
}
request.getRequestDispatcher("fileUpDownload.jsp").forward(request, response);
}else if(("showFiles").equals(op)){
//得到上传到服务器的文件夹
String realPath = this.getServletContext().getRealPath("smartUpdownload");
//得到所有文件的文件名,并显示在页面上
File file = new File(realPath);
File[] listFiles = file.listFiles();
ArrayList<String> list = new ArrayList<String>();
for (File fi : listFiles) {
list.add(fi.getName());//把所有的文件名存入集合
}
request.setAttribute("list", list);
request.getRequestDispatcher("fileUpDownload.jsp").forward(request, response);
}else if(("download").equals(op)){
su.initialize(getServletConfig(), request, response);//初始化
su.setContentDisposition(null);
String filename1 = request.getParameter("filename");
String filename = new String(filename1.getBytes("ISO-8859-1"), "GB2312");
System.out.println(filename);
try {
su.downloadFile("/smartUpdownload/"+filename);
} catch (SmartUploadException e) {
e.printStackTrace();
}
} } }

jsp 内容如下:

这里使用到了  c 标签,所以需要两个jar包  jstl.jar 和   standard-1.1.2.jar

并使用

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 导入包
 <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="org.lxh.smart.SmartUpload"%>
<%@page import="org.lxh.smart.File"%>
<%
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 'fileUpDownload.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="FileUpdownLoad?op=upload" method="post" enctype="multipart/form-data" >
<input type="file" name="file1" value="上传的文件">
<input type="submit" value="上传">
</form>
<hr/>
<a href = "FileUpdownLoad?op=showFiles">显示下载的文件目录</a><br/>
<c:forEach items="${list}" var="filename">
<a href="FileUpdownLoad?op=download&filename=${filename }">下载文件:${filename }</a><br/>
</c:forEach> <a href="ImageServlet">显示水印图片</a>
</body>
</html>

java 文件的上传和下载的更多相关文章

  1. Java 文件本地上传、下载和预览的实现

    以下方法为通用版本 实测图片和pdf 都没有问题 上传方法需要前端配合post请求 ,下载前端用a标签就可以,预览 前端使用ifrme标签   ,就可以实现基本功能... 1.文件本地上传 publi ...

  2. java实现ftp文件的上传与下载

    最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...

  3. 初学Java Web(7)——文件的上传和下载

    文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...

  4. java web(四):request、response一些用法和文件的上传和下载

    上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...

  5. java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  6. java客户端文件的上传和下载

    java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...

  7. Java中文件的上传与下载

    文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar  commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...

  8. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  9. Apache FtpServer 实现文件的上传和下载

    1 下载需要的jar包 Ftp服务器实现文件的上传和下载,主要依赖jar包为: 2 搭建ftp服务器 参考Windows 上搭建Apache FtpServer,搭建ftp服务器 3 主要代码 在ec ...

随机推荐

  1. 解决wordcloud的一个error:Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

    环境: 操作系统:Windows 7 64位 语言:Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:17:26) w ...

  2. thinkpadT480安装archlinux

    制作U盘启动盘 下载archlinux镜像 下载powerison 将镜像刻录到U盘中 设置bios 如果已经安装了win8+系统, 开机重启然后按f1,进入设置界面,加载启动项中将usb放到最前 保 ...

  3. [CoffeeScript]在WebStorm里运行CoffeeScript

    CoffeeScript 是一门编译到 JavaScript 的小巧语言. 在 Java 般笨拙的外表下, JavaScript 其实有着一颗华丽的心脏. CoffeeScript 尝试用简洁的方式展 ...

  4. Go语言特殊函数介绍

    main 函数 Go语言程序的默认入口函数(主函数):func main()函数体用{}一对括号包裹.只能应用于package main func main(){ //函数体 } init 函数 go ...

  5. java高并发测试代码

    package com.example.test; import java.net.URL;import java.net.URLConnection;import java.util.concurr ...

  6. windows本地提权——2003笔记

    windows主机 用户有三种:Users,Administrator,System 本地拥有administrator权限用户提权到system用户 win2003: at命令 系统调度任务的指令, ...

  7. 反弹shell集锦

    1.   关于反弹shell 就是控制端监听在某TCP/UDP端口,被控端发起请求到该端口,并将其命令行的输入输出转到控制端.reverse shell与telnet,ssh等标准shell对应,本质 ...

  8. my.等级限制

    1.20190405 “春之恋曲 4月5日双平台新服开服公告”,20190426  上去新建了一个号  发现等级限制是 66级(2天后开启新等级) 20190412 “胭脂雪 4月12日双平台新服开服 ...

  9. Robot Framework(AutoItLibrary库关键字介绍)

    AutoItLibrary库关键字 AutoItLibrary 的对象操作大体上有几大主要部分,Window 操作.Control 操作.Mouse 操作.Process操作.Run 操作.Reg 操 ...

  10. ios UITableView 搜索

    自己实现 UITableView 搜索,相对于使用 UISearchDisplayController 来说自己写稍微麻烦了那么一点点,但是更加灵活.主要就是用一个字段区分出当前是搜索还是非搜索,然后 ...