SpringMVC的文件上传形式
@Controller
public class UploadController {
/*
* 单个文件上传
*/
@RequestMapping("/uploadFile")//
public <HttpServletRequest> String uploadFile(@RequestParam("file")MultipartFile multipartFile,javax.servlet.http.HttpServletRequest request){
System.out.println("UploadController.uploadFile()");
System.out.println(multipartFile.getOriginalFilename());
//上传文件的路径
String uploadPath="D:/eclipse/eclipsedata/SpringMVC_4_upload/WebContent/upload";
// String uploadpaht = request.getSession().getServletContext().getRealPath("upload");
try {
FileCopyUtils.copy(multipartFile.getInputStream(), new FileOutputStream(uploadPath+File.separatorChar+multipartFile.getOriginalFilename()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "/ok";
}
// 多个文件上传
@RequestMapping("/uploadFile2")//
public <HttpServletRequest> String uploadFile2(@RequestParam("file")MultipartFile[] multipartFile,javax.servlet.http.HttpServletRequest request){
System.out.println("UploadController.uploadFile()");
// System.out.println(multipartFile[].getOriginalFilename());
//上传文件的路径
String uploadPath="D:/eclipse/eclipsedata/SpringMVC_4_upload/WebContent/upload";
//String uploadpaht = request.getServletContext().getRealPath("upload");
for (int i = 0; i < multipartFile.length; i++) {
try {
//工具类进行拷贝
FileCopyUtils.copy(multipartFile[i].getInputStream(), new FileOutputStream(uploadPath+File.separatorChar+multipartFile[i].getOriginalFilename()));
System.out.println(multipartFile[i].getOriginalFilename());//输出文件的名字
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return "/ok";
}
}
文件下载
@Controller
public class DownLoadController {
@RequestMapping("/download")
public void download(HttpServletRequest request,HttpServletResponse response){
//定义文件下载路径
String downPath="D:/aaa/SpringMVC_4_downLoad/WebContent/upload/d.jpg";
//配置响应头文件
String fileName="索隆.png";
response.setContentType("application/x-msdownload");
try {
response.setHeader("Content-Disposition", "attachment;filename=" +new String(fileName.getBytes("utf-8"),"iso-8859-1"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//读取文件
try {
FileInputStream iStream=new FileInputStream(downPath);
FileCopyUtils.copy(iStream, response.getOutputStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Struts2中文件上传下载的方式;
上传单个文件的代码
public class FileUpload extends ActionSupport {
private File file;
private String fileFileName;//定义文件名字,struts会对文件自动赋值
private String fileContentType;
public String execute(){
System.out.println("FileUpload.execute()");
// System.out.println(file);
String uploadPath="D:/movie/upload/";//文件上传的路径
String fileName="a.jpg";
//FileUtils.copyDirectory(srcDir, destDir);src:源文件路径,dest:目标文件路径
try {
FileUtils.copyFile(file, new File(uploadPath+fileFileName));
} catch (IOException e) {
e.printStackTrace();
}
return Action.SUCCESS; }
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
}
上传的Jsp是:
<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name ="file" label="上传文件"/>
<s:file name ="file" label="上传文件"/>
<s:submit value="上传"/>
</s:form>
上传多个文件
public class FileUpload2 extends ActionSupport {
private File[] file;
private String[] fileFileName;
private String fileContextType;
public String execute(){
System.out.println("FileUpload2.execute()");
String uploadPath="D:/movie/upload/";
try {
for(int i=0;i<file.length;i++){
FileUtils.copyFile(file[i], new File(uploadPath+getFileFileName()[i]));
}
} catch (IOException e) {
e.printStackTrace();
}
return Action.SUCCESS;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContextType() {
return fileContextType;
}
public void setFileContextType(String fileContextType) {
this.fileContextType = fileContextType;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
}
下载文件
public class FileDownLoad {
private InputStream ips;//构建下载文件的输入流
private String fileName;
public String execute(){
System.out.println("FileDownLoad.execute()");
//设置下载文件路径
String downFile="D:/movie/a.txt";
try {
ips=new FileInputStream(new File(downFile));
fileName="a.txt";//下载后文件名称
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return Action.SUCCESS;
}
public InputStream getIps() {
return ips;
}
public void setIps(InputStream ips) {
this.ips = ips;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}