• struts2上传多文件


    public class UploadAction {
    
    	private static final long serialVersionUID = 7738910297605127355L;
    	//private News news; //注入实体对象
    	private static final int BUFFER_SIZE = 16 * 1024;// 缓冲-上传字节数组
    	private String upfileName;
    
    	private List<File> uploads = new ArrayList<File>();
    	private List<String> uploadFileNames = new ArrayList<String>();
    	private List<String> uploadContentTypes = new ArrayList<String>();
    
    	public String add() {
    		int size = uploadContentTypes.size();
    		if (size > 0) {
    			for (int i = 0; i < size; i++) {
    				String contentype = uploadContentTypes.get(i);
    				if (contentype.indexOf("image") > -1) {
    					File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/upload/images")+ "/" + uploadFileNames.get(i));
    					UploadUtil.copy(uploads.get(i), imageFile, BUFFER_SIZE);
    					String picturePath = "/upload/images" + "/" + uploadFileNames.get(i);
    					String picName = uploadFileNames.get(i);
    					
    					// 设置实体对象中的图片地址
    					// news.setPicturePath(picturePath);
    					// news.setPictureName(uploadFileNames.get(0));
    
    					System.out.println("图片地址:" + picturePath);
    					System.out.println("图片名称:" + picName);
    				} 
    				/*否则就是上传附件,不是图片
    				else {
    					File fujianFile = new File(ServletActionContext.getServletContext().getRealPath("/upload")+ "/" + uploadFileNames.get(1));
    					UploadUtil.copy(uploads.get(1), fujianFile, BUFFER_SIZE);
    					// 设置实体对象中的图片地址
    					// news.setUpfileName(uploadFileNames.get(1));
    					String picturePath = "/upload/images" + "/"
    							+ uploadFileNames.get(0);
    					String picName = uploadFileNames.get(0);
    					System.out.println("图片地址:" + picturePath);
    					System.out.println("图片名称:" + picName);
     				}
     				*/
    			}
    		}
    		return "upload";
    	}
    
    	public String getUpfileName() {
    		return upfileName;
    	}
    
    	public void setUpfileName(String upfileName) {
    		this.upfileName = upfileName;
    	}
    
    	public List<File> getUpload() {
    		return this.uploads;
    	}
    
    	public void setUpload(List<File> uploads) {
    		this.uploads = uploads;
    	}
    
    	public List<String> getUploadFileName() {
    		return this.uploadFileNames;
    	}
    
    	public void setUploadFileName(List<String> uploadFileNames) {
    		this.uploadFileNames = uploadFileNames;
    	}
    
    	public List<String> getUploadContentType() {
    		return this.uploadContentTypes;
    	}
    
    	public void setUploadContentType(List<String> contentTypes) {
    		this.uploadContentTypes = contentTypes;
    	}
    
    }
    
     
    UploadUtil.java
    -------------------
    /**
     * 上传文件,名称以日期命名
     * 
     * @author Administrator
     */
    public class UploadUtil {
    	/**
    	 * 上传文件
    	 * 
    	 * @param src
    	 *            源文件
    	 * @param dst
    	 *            目标文件
    	 * @param BUFFER_SIZE
    	 *            缓冲大小
    	 */
    	public static void copy(File src, File dst, final int BUFFER_SIZE) {
    		try {
    			InputStream in = null;
    			OutputStream out = null;
    			try {
    				in = new BufferedInputStream(new FileInputStream(src));
    				dst = rename(dst);
    				out = new BufferedOutputStream(new FileOutputStream(dst));
    				int word = 0;
    				while ((word = in.read()) != -1) {
    					out.write(word);
    				}
    			} finally {
    				if (null != in) {
    					in.close();
    				}
    				if (null != out) {
    					out.close();
    				}
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * 得到扩展名
    	 * @param fileName
    	 * @return
    	 */
    	public static String getExt(String fileName) {
    		int pos = fileName.lastIndexOf(".");
    		return fileName.substring(pos);
    	}
    
    	/**
    	 * 更改上传文件名
    	 * @param file 文件对象
    	 * @return 更名后的文件对象
    	 */
    	public static File rename(File file) {
    		String fileName = "";// 文件名
    		String extName = "";// 文件扩展名
    		Calendar cal = Calendar.getInstance();
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    		fileName = formatter.format(cal.getTime()) + (int) (Math.random() * 10);
    		extName = getExt(file.getName());
    		String newName = fileName + "." + extName;
    		file = new File(file.getParent(), newName);
    		return file;
    	}
    
    }
    

      

     upload.jsp
    <html>
      <body>
       <s:form method="post" action="jupload" enctype ="multipart/form-data">
        上传图片:<s:file name ="upload" theme="simple"/> 
        <br/>
        上传附件:<s:file name ="upload" theme="simple"/>
        <br/> 
        <s:submit theme="simple" value="提交"/>
      </s:form>
      </body>
    </html>
    

      struts.xml

    <!-- 上传文件 cyjch -->
    <action name="jupload"  method="add" class="upAction" >
    	<result name="upload">upload.jsp</result>
    </action>
    

      

      

  • 相关阅读:
    SE Springer小组之《Spring音乐播放器》需求分析说明书一
    SE Springer小组之《Spring音乐播放器》可行性研究报告一、二(转载)
    SE Springer小组之《Spring音乐播放器》可行性研究报告五、六
    软件分析之QQ
    浅谈《软件工程》
    前端格式
    Django基础
    正则表达式
    任务-线程
    一名奔三的程序猿的困惑
  • 原文地址:https://www.cnblogs.com/cyjch/p/2360279.html
Copyright © 2020-2023  润新知