下载和上传(其实就是复制粘贴)
下载:
/*
* 说⽩了其实导出就是复制粘贴(从存储在云上的⽂件复制到⽤户的本地硬盘上):
* 先取得路径,然后根据路径来复制 BufferedInputStream 是为了更⾼效的复制,read()⽅法是具体的复制⾏为
* 把流写出中,write()就是具体的粘贴⾏为
*/
public class HaHa {
// 下载excel 模板
public Response downLoadExcelModel(HttpServletResponse response,HttpServletRequest request) {
BufferedInputStream bufferedInputStream;
FileInputStream fileInputStream;
OutputStream outputStream;
try {
String modelName = Parameter("modelName");
String modelName1 = de(modelName, "UTF-8").replaceAll("%28", "\\(").replaceAll("%29", "\\)");// 转义符
String pathName = "excel/" + modelName + ".xlsx";
// path 输出值为:E:\20171128\esim-80002795\esim-service\esim\esim-web\src\main\webapp\excel\险种组合管理表.xlsx 记住,这个⽅法⼀般都是在src.main.webapp下⾯的 String path = Session().getServletContext().getRealPath(pathName);
File file = new File(path);
fileInputStream = new FileInputStream(file);// 如果该路径下⾯没有该⽂件,则报 “系统不到指定路径” 的错误
bufferedInputStream = new BufferedInputStream(fileInputStream);// 记住,BufferedInputStream构造⽅法的参数⼀般都是FileInputStream对象byte[] b = new byte[bufferedInputStream.available()];
* 利⽤response得到输出流outputStream
* 再⼀次性把这输出流写出
*/
response.setContentType("application/vnd.ms-excel");//contentType⾥⾯的内容要对应excel格式的啊
response.setHeader("Content-disposition", "attachment;filename=" + modelName1 + ".xlsx");//modelName1⼀定要是那种编码之后的格式
outputStream = OutputStream();
outputStream.write(b);
} catch (UnsupportedEncodingException e) {
<("下载模版⽂件名转换异常" + e);
} catch (FileNotFoundException e) {
<("下载模版⽂件没有到对应的模版名异常" + e);
} catch (IOException e) {
<("下载模版⽂件名IO异常" + e);
} finally {
bufferedInputStream.close();
fileInputStream.close();// 我记得这个是不⽤关的
outputStream.flush();
outputStream.close();
}
}
/* 下载 wmv 或者 MP4 ⽂件
* 因为mp4或者wmv的⽂件⽐较⼤,所以最好不要⽤availalbe⽅法啊
*/
public Response downLoadSystemVideo(HttpServletResponse response,HttpServletRequest request,String modelName){
String pathName="video/"+modelName;
String[] split = modelName.split("\\.");//转义符,实际上是对 . 进⾏ split;因为要下载的⽂件后缀有wmv 以及 mp4 嘛
String Session().getServletContext().getRealPath(pathName);
File file = new File(path);
try(
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);) {
if("wmv".equals(split[split.length - 1])) {
response.setContentType("wmv video/x-ms-wmv");//contentType⾥⾯的内容要对应wmv格式的啊
String modelName1 = de(modelName, "UTF-8").replaceAll("%28", "\\(").replaceAll("%29", "\\)");
response.setHeader("Content-disposition", "attachment;filename="+modelName1);
}else if("mp4".equals(split[split.length - 1])) {
response.setContentType("mp4 video/mpeg4");//contentType⾥⾯的内容要对应mp4格式的啊
String modelName1 = de(modelName, "UTF-8").replaceAll("%28", "\\(").replaceAll("%29", "\\)");
response.setHeader("Content-disposition", "attachment;filename="+modelName1);
}
OutputStream outputStream = OutputStream();
byte[] bytes = new byte[2000];
int a;
while((a = ad(bytes)) != -1) {
outputStream.write(bytes, 0, a);
}
bufferedInputStream.close();
outputStream.flush();
outputStream.close();
} catch (UnsupportedEncodingException e) {
<("下载视频⽂件名转换异常"+e);
} catch (FileNotFoundException e) {
<("下载视频⽂件没有到对应的视频名异常"+e);
} catch (IOException e) {
<("下载视频⽂件名IO异常"+e);
}
return null;
}
}
上传:
/
***
* localhost:8080/testWeb/home.jsp
* @author 80002795
*
*/
@Controller
@RequestMapping("upload")
public class Upload {
/*
* 通过流的⽅式上传⽂件
* @RequestParam("file") 将name=file控件得到的⽂件封装成CommonsMultipartFile 对象
*/
@RequestMapping("fileUpload")
public void fileUpload(@RequestParam("file01") CommonsMultipartFile commonsMultipartFile) throws IOException {
InputStream is = null;
OutputStream os = null;
System.out.OriginalFilename());// 员⼯信息安全⼿册.pdf
System.out.FileItem().getName());// C:\Users\Public\Desktop\员⼯信息安全⼿册.pdf
System.out.ContentType());// application/pdf
long startTime=System.currentTimeMillis();
try {
InputStream();//哈哈,通过这个就可以获取输⼊流了,说⽩了通过这个⽅法就是复制了
//得到输出流
//OriginalFilename()这个⽅法是获取导⼊⽂件的⽂件名,说⽩了下⾯的代码就是得到⽂件名,⽤时间肯定要保持数据的独⽴性啊
String childFileName = new SimpleDateFormat("yyyyMMddHHmmss").Instance().getTime()) + OriginalFilename();
System.out.println(childFileName);
//导⼊之后,要存进哪⾥的路径,如果没有这个路径,就创建⼀个呗
String parentPath = "E:\\upload\\upload22\\upload33";
File filePart = new File(parentPath);
if (!ists()) {
replaceall()
filePart.mkdirs();
}
File fileAll = new File(filePart,childFileName);//记得,关于这个构造⽅法,第⼀个参数可以有两种形式,⼀种是File对象,⼀种是string类型的路径
os=new FileOutputStream(fileAll); //获取输出流
// 粘贴输⼊流到输出流
int temp;
while((ad()) != -1){ //⼀个⼀个字节的读取并写⼊
os.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
is.close();
os.flush();
os.close();
}
long endTime=System.currentTimeMillis();
System.out.println("⽅法⼀的运⾏时间:"+String.valueOf(endTime-startTime)+"ms");// 得到的时间⼤概是:14850ms,实在太慢了
}
/*
* 采⽤file.Transto 来保存上传的⽂件
*/
@RequestMapping("fileUpload02")
public void fileUpload02(@RequestParam("file02") CommonsMultipartFile commonsMultipartFile, HttpServletRequest request) throws IOException {
long startTime=System.currentTimeMillis();
try {
//除了上传⽂件,还要得到其它参数啊
String name = Parameter("name");
//获得⽂件名并且得到指定路径
//OriginalFilename()这个⽅法是获取导⼊⽂件的⽂件名,说⽩了下⾯的代码就是得到⽂件名,⽤时间肯定要保持数据的独⽴性啊
String childFileName = new SimpleDateFormat("yyyyMMddHHmmss").Instance().getTime()) + OriginalFilename();
//导⼊之后,要存进哪⾥的路径,如果没有这个路径,就创建⼀个呗
String parentPath = "E:\\upload\\upload22\\upload33";
File filePart = new File(parentPath);
if (!ists()) {
filePart.mkdirs();
}
File fileAll = new File(filePart,childFileName);//记得,关于这个构造⽅法,第⼀个参数可以有两种形式,⼀种是File对象,⼀种是string类型的路径
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("⽅法⼆的运⾏时间:"+String.valueOf(endTime-startTime)+"ms");// 得到的时间⼤概是:10ms
}
/*
*采⽤spring提供的上传⽂件的⽅法,这种⽅法⼜长⼜丑,都不想看了
*/
@RequestMapping("springUpload")
public void springUpload(HttpServletRequest request) throws IllegalStateException, IOException {
long startTime=System.currentTimeMillis();
CommonsMultipartResolver multipartResolver=new Session().getServletContext());//将当前上下⽂初始化给 CommonsMutipartResolver (多部分解析器)
//检查form中是否有enctype="multipart/form-data"
if(multipartResolver.isMultipart(request)){
//将request变成多部分request
MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
//获取multiRequest 中所有的⽂件名
Iterator FileNames();
while(iter.hasNext()){
//⼀次遍历所有⽂件
MultipartFile ().toString());
if(file!=null){
String path="E:/springUpload"+OriginalFilename();
File file2 = new File(path);
}
}
}
long endTime=System.currentTimeMillis();
System.out.println("⽅法三的运⾏时间:"+String.valueOf(endTime-startTime)+"ms");//输出9ms
}
@RequestMapping("getMultipartFile")
public void getMultipartFile(MultipartFile multipartFile) throws Exception {
long startTime=System.currentTimeMillis();
try {
// String fileName = Name();
String parentPath = "E:\\upload\\upload22\\upload33";
File filePart = new File(parentPath);
if (!ists()) {
filePart.mkdirs();
}
File fileAll = new File(filePart,"444444");//记得,关于这个构造⽅法,第⼀个参数可以有两种形式,⼀种是File对象,⼀种是string类型的路径
} catch (Exception e) {
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("⽅法四的运⾏时间:"+String.valueOf(endTime-startTime)+"ms");
}
}
END
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论