作者:钢铁猪991884679 | 来源:互联网 | 2023-09-25 19:41
注:eclipse开发工具 <context:component-scanbase-package"com.zhouwu
注:eclipse 开发工具 加入后 controller包的控制类会有S符好
commons io 和commons upload 开发
FTP服务器创建和上下传文件(3)
httpclient调用代码
package com.zhouwuji.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
public class FileController {
@RequestMapping("/download.do")
public void doMain(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//得到要下载的文件名
String fileName =request.getParameter("filename");
System.out.println("fileName:"+fileName.trim());
//23239283-92489-阿凡达.avi
// fileName="zhangruan.txt";
// fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
//上传的文件都是保存在/WEB-INF/upload目录下的子目录当中
String fileSaveRootPath=request.getServletContext().getRealPath("/WEB-INF/upload");
//得到要下载的文件
File file = new File(fileSaveRootPath + "\\" + fileName);
//如果文件不存在
if(!file.exists()){
request.setAttribute("message", "您要下载的资源已被删除!!");
request.getRequestDispatcher("/fail.jsp").forward(request, response);
return;
}
//处理文件名
String realname = fileName.substring(fileName.indexOf(".")+1);
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(file);
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
System.out.println("下载成功");
//关闭文件输入流
in.close();
//关闭输出流
out.close();
}
@RequestMapping("/upload.do") //@RequestParam("uploadfile")
@ResponseBody
public String queryFileData(@RequestParam("uploadfile")CommonsMultipartFile file,HttpServletRequest request){
System.out.println("+++++++++++++++++"+request.getParameter("username"));
/*MultipartFile是对当前上传的文件的封装,
* 当同时上传多个文件时,可以给定多个MultipartFile参数(数组)
*/
if(file!=null){
System.out.println("文件对象接到了!");
//获取文件名
String filename=file.getOriginalFilename();
//获取上传路径
String path=request.getSession().getServletContext().getRealPath("/WEB-INF/upload/"+filename.trim());
System.out.println("path:"+path);
//创建文件流并指定写入路径
File destFile=new File(path);
//springmvc的方式
//该方法自动操作,不需要额外的去关闭IO流
//复制临时文件到指定文件夹下
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
System.out.println("上传成功");
return "/success.jsp";
} catch (IOException e) {
e.printStackTrace();
System.out.println("上传失败");
return "/error.jsp";
}
}else{
System.out.println("文件没有对象接到了!");
return "/error.jsp";
}
}
}
/*文件对象接到了!
filename:中软笔记.txt
path:D:/apache-tomcat-7.0.82-windows-x64/apache-tomcat-7.0.82/webapps/project-ajaxjson/upload/中软笔记.txt
上传成功*/
FileController