package com.lanou.strusUpdate;
import java.io.File;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class uploadAction extends ActionSupport {
File[] liguocheng; String[] liguochengFileName; InputStream is; public String upload() throws IOException{ /*try { FileInputStream fis=new FileInputStream(liguocheng); String path=ServletActionContext.getServletContext().getRealPath("/upload/"); System.out.println(path); FileOutputStream fos=new FileOutputStream(path+"/"+liguochengFileName,true); byte[] b=new byte[1024]; int l=-1; while(fis.read(b)>0){ fos.write(b); } fos.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ for(int i=0;i<liguocheng.length;i++){ String path=ServletActionContext.getServletContext().getRealPath("/upload/"); FileUtils.copyFile(liguocheng[i], new File(path+"/"+liguochengFileName[i])); } /*int a=liguochengFileName.lastIndexOf("."); String strname1= liguochengFileName.substring(0,a); String strname2=liguochengFileName.substring(a);*/ //String newPath=strname1+System.currentTimeMillis()+strname2; System.out.println(System.currentTimeMillis()); return SUCCESS; } public String download() throws FileNotFoundException{ String path=ServletActionContext.getServletContext().getRealPath("/upload/"); is=new FileInputStream(path+"/"+"1.jpg"); String str="pc.jpg"; ActionContext.getContext().getValueStack().set("down", str); return SUCCESS; }public File[] getLiguocheng() {
return liguocheng; }public void setLiguocheng(File[] liguocheng) {
this.liguocheng = liguocheng; }public String[] getLiguochengFileName() {
return liguochengFileName; }public void setLiguochengFileName(String[] liguochengFileName) {
this.liguochengFileName = liguochengFileName; }public InputStream getIs() {
return is; }public void setIs(InputStream is) {
this.is = is; } }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!--ture代表启动 false停止 --> <constant name="struts.devMode" value="ture" /><package name="default" namespace="/" extends="json-default">
<action name="fileupload" class="com.lanou.strusUpdate.uploadAction" method="upload"> <result name="success"> seccess.html </result> </action> <action name="fileDownload" class="com.lanou.strusUpdate.uploadAction" method="download"> <result name="success" type="stream"> <param name="inputName">is</param> <param name="contentDisposition">attachment;filename=${down}</param> </result> </action> </package> </struts>
<!DOCTYPE html>
<html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body><form action="fileupload" method="post" enctype="multipart/form-data">
请选择要上传的文件: <input type="file" name="liguocheng"> <br> 请选择要上传的文件: <input type="file" name="liguocheng"> <br> 请选择要上传的文件: <input type="file" name="liguocheng"> <br> 请选择要上传的文件: <input type="file" name="liguocheng"> <br> 请选择要上传的文件: <input type="file" name="liguocheng"> <input type="submit"> </form> </body> </html>
<!DOCTYPE html>
<html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 成功 <a href="fileDownload">下载文件</a> </body>