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";
??//配置響應(yīng)頭文件
??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;//構(gòu)建下載文件的輸入流
?private String fileName;
?public String execute(){
??System.out.println("FileDownLoad.execute()");
??//設(shè)置下載文件路徑
??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;
?}
}
?