upload&&download
package am.demo;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("serial")
public class Upload extends HttpServlet {
private String uploadPath = "d://temp"; // 上传文件的目录
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
response.setContentType("text/html;charset=GBK");
response.getWriter().print(
"<mce:script language='javascript'><!--
alert('上传成功');window.location.href='index.jsp';
// --></mce:script>");
} catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
}
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
}
}
package am.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Download extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aFileName = new String(request.getParameter("name").getBytes(
"iso8859-1"), "gbk");
File fileLoad = new File("d:/temp", aFileName);
FileInputStream in = null; // 输入流
OutputStream out = response.getOutputStream();
byte b[] = new byte[1024];
try {
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(aFileName.getBytes("GBK"), "ISO-8859-1"));
// download the file.
in = new FileInputStream(fileLoad);
int n = 0;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
package am.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Delete extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws FileNotFoundException, IOException {
String aFileName = new String(request.getParameter("name").getBytes(
"iso8859-1"), "gbk");
File file = new File("d:/temp", aFileName);
response.setContentType("text/html;charset=GBK");
if (!file.isDirectory()) {
file.delete();
response.getWriter().print(
"<mce:script language='javascript'><!--
alert('删除成功');window.location.href='index.jsp';
// --></mce:script>");
} else {
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
upload&&download的更多相关文章
- ideaJ+maven+javaweb实践: sevlet实现upload&download,javaIO代码
因为工作的机器不让拷贝出来也不让发邮件出来也不让访问外网,所以文件两个PC挪来挪去很麻烦. 决定写一个网页,只有upload和download ideaJ,maven,java,tomcat 写一个j ...
- Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )
更新 : 2018-01-22 之前漏掉了一个 image 优化, 就是 progressive jpg refer : http://techslides.com/demos/progressi ...
- file upload download
1. 文件上传与下载 1.1 文件上传 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 文件上传,要点: 前台: ...
- Upload/download/UrlConnection/URL
文件上传的核心点 1:用<input type=”file”/> 来声明一个文件域.File:_____ <浏览>. 2:必须要使用post方式的表单. 3:必须设置表单的类型 ...
- WCF传输大数据 --断点续传(upload、download)
using System; using System.IO; using System.Runtime.Serialization; using System.ServiceModel; namesp ...
- jQuery File Upload 单页面多实例的实现
jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...
- jQuery File Upload blueimp with struts2 简单试用
Official Site的话随便搜索就可以去了 另外新版PHP似乎都有问题 虽然图片都可以上传 但是response报错 我下载的是8.8.7木有问题 但是8.8.7版本结合修改main. ...
- AFNetworking 3.0 源码解读(五)之 AFURLSessionManager
本篇是AFNetworking 3.0 源码解读的第五篇了. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilityManager AFNetworking 3 ...
- 基于Spring Mvc实现的Excel文件上传下载
最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库.因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例. 基础框架 之前曾经介绍过一个最简单的spring ...
随机推荐
- Android开发的菜鸟小记
1.主线程异常:添加网络连接: 2.权限异常: 3.空指针异常:NullException: 添加网络权限: DEBUG:Connected to the target VM, address: 'l ...
- APIPA(Automatic Private IP Addressing,自动专用IP寻址)
APIPA APIPA(Automatic Private IP Addressing,自动专用IP寻址),是一个DHCP故障转移机制.当DHCP服务器出故障时, APIPA在169.254.0.1到 ...
- 51nod 1005 大数加法
#include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...
- 83 parrted-分区和分区大小的调整
parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小.作为一种设计用于Linux的工具,它没有构建成处理与fdisk关联的多种分区类型, ...
- python基础-RE正则表达式
re 正则表示式 正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写 ...
- [fiddler] 手机抓包
最近工作涉及到与原生app联调,需要抓取手机上的请求.借此机会研究了下fiddler,简直神器. 以下简单介绍通过fiddler进行手机抓包的方法,之后也会陆续更新fiddler的其他功能 设置fid ...
- Google开源库-Volley的使用
一.什么是Volley? Volley is an HTTP library that makes networking for Android apps easier and most import ...
- python 下 tinker、matplotlib 混合编程示例一个
该例是实现了 Tinker 嵌入 matplotlib 所绘制的蜡烛图(k 线),数据是从 csv 读入的.花一下午做的,还很粗糙,仅供参考.python 代码如下: import matplotli ...
- linux配置java开发环境
一.下载jdk java -version 查看有没有安装jdk 下载对应版本的jdk:jdk-java7u60-linux-x64.tar.gz 二.解压 cp jdk-java7u60-lin ...
- Java数组及其内存分配
几乎所有的程序设计语言都支持数组.Java也不例外.当我们需要多个类型相同的变量的时候,就考虑定义一个数组.在Java中,数组变量是引用类型的变量,同时因为Java是典型的静态语言,因此它的数组也是静 ...