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 ...
随机推荐
- 打扮IDEA更换主题
原文链接:https://blog.csdn.net/github_39577257/article/details/80629750 当我们安装一个新的IDEA工具时,第一次进入时会提示我们选择一个 ...
- thinkphp3.2.3----图片上传并生成缩率图
public function uploadify(){ if(!IS_POST){ $this->error('非法!'); } $upload = $this->_upload(); ...
- java使用freemarker导出复杂的excel表格
正常导出excel表格使用的poi,但是导出复杂的excel有点困难,但是可以使用freemaker模板来导出复杂的excel. 1.都是先生成一个Excel表格的模板,最好是增加一行数据.具体看图里 ...
- nodemon详解
1.介绍 Nodemon是一个使用工具,它将会见监视源文件中任何的更改并自动重启服务器.Nodemon不会对你的代码产生额外的更改,它只是node命令的替代品.因为当你修改源文件后,如果你用的是原来的 ...
- python全栈开发_day4_if,while和for
一.if 1)if的用途 if常用于判断. 2)if的语法 tag=True tag2=True if tag: print("代码") elif tag2: print(&quo ...
- scikit-learn中predict_proba用法 (与predict的区别)
predict_proba返回的是一个 n 行 k 列的数组, 第 i 行 第 j 列上的数值是模型预测 第 i 个预测样本为某个标签的概率,并且每一行的概率和为1. # conding :utf-8 ...
- 固定定位 z-index iconfont的使用 043
固定定位 现象 脱标 提升层级 将盒子固定住 参考点 浏览器的左上角 : <!DOCTYPE html> <html lang="en"> <he ...
- 封装通用的xhr对象(兼容各个版本)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue项目element-ui框架中的弹窗中的表单验证清除问题
问题回顾: 1.vue项目的在弹窗上的form表单验证,第一次点击新增时正常,第二次新增打开弹窗后由于表单内容为空,出现验证这种情况 2.为了解决上面的情况,在执行点击新增事件加上this.$refs ...
- javascript 正则表达式校验方式写法
if(/[0-9]/.test(value)){return true; } if(/[a-z]/.test(value)){return true; } if(/[A-Z]/.test(value) ...