java 文件的上传和下载
主要介绍使用 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 文件的上传和下载的更多相关文章
- Java 文件本地上传、下载和预览的实现
以下方法为通用版本 实测图片和pdf 都没有问题 上传方法需要前端配合post请求 ,下载前端用a标签就可以,预览 前端使用ifrme标签 ,就可以实现基本功能... 1.文件本地上传 publi ...
- java实现ftp文件的上传与下载
最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...
- 初学Java Web(7)——文件的上传和下载
文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...
- java web(四):request、response一些用法和文件的上传和下载
上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...
- java实现文件的上传和下载
1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...
- java客户端文件的上传和下载
java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...
- Java中文件的上传与下载
文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...
- 在SpringMVC框架下实现文件的 上传和 下载
在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...
- Apache FtpServer 实现文件的上传和下载
1 下载需要的jar包 Ftp服务器实现文件的上传和下载,主要依赖jar包为: 2 搭建ftp服务器 参考Windows 上搭建Apache FtpServer,搭建ftp服务器 3 主要代码 在ec ...
随机推荐
- java处理excel-xlsx格式大文件的解决方案
1.第一次读取7M左右的ecxel文件,使用poi 库实现,参考了下面的博文. http://www.cnblogs.com/chenfool/p/3632642.html 使用上面的方法在 下面Wo ...
- 看完MJ讲解的单例后的个人总结
1.单例的介绍 单例是iOS常用的开发模式的一种. 2.什么是单例 单例就是一个类只创建一个对象,只分配一次内存空间. 3.单例的应用场景 1)系统的单例: [UIApplication share ...
- Python环境安装与升级
Python是跨平台的,它可以运行在Windows,Mac,Linux/Unix系统上,在Windows上写的Python程序,在Linux上也是能够运行的.目前,Python有两个大版本,一个是2. ...
- .net core webapi发布
地址:https://www.cnblogs.com/laozhang-is-phi/p/9565227.html#autoid-5-1-0 地址2:https://www.cnblogs.com/f ...
- PowerBuilder中新建PBL
首先需要打开工作区间workspace , 然后选择你的第一个根pbl文件. 如图所示: 选择WorkSpace下的第一个application.PBT文件,然后单击鼠标右键,选择属性Properti ...
- python 封装,隐藏属性,绑定方法classmethod和staticmethod
[封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 1. 将变化隔离: 2. 便于使用: 3. 提高复用性: 4. 提高安全性: [封装原则] 1. 将不需要对外提供的内容都隐藏起 ...
- linux curl post/put请求
案列: -X: 请求方式 --header: 请求header -d: 请求的数据 最后跟上请求的地址 curl -X PUT --header 'Content-Type: application/ ...
- 怎么在vue中引入layui
新项目想用layui框架,学习了把前辈是怎么引入layui的,这里记录下 1.index.html要引入layui.js文件 <script src="/static/layui/la ...
- Linux 系统下安装 mysql5.7.25(glibc版)
前言:经过一天半的折腾,终于把 mysql 5.7.25 版本安装上了 Amazon Linux AMI release 2017.09 系统上,把能参考的博客几乎都看了一遍,终于发现这些细节问题,然 ...
- unity 渲染第一步
unity 不是将宇宙投影到水晶球里,而是:将整个 view frustum 投影成 一个 cube .------ <unity 渲染箴言> 观察一下,整个 view frustum 以 ...