strus2使用commons-fileupload-1.3.1.jar工具包的图片上传

本文详细介绍了如何在Struts2框架中利用commons-fileupload工具包实现文件上传功能,包括依赖包的引入、代码实现以及成功上传图片后的效果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

commons-fileupload-1.3.1.jar工具包的依赖包是commons-io-2.2.jar(FileUploaddepends on Commons IO, so make sure you have the version mentioned on thedependenciespage in your classpath before continuing.)

 

commons-fileupload-1.3.1.jar

在官网的wiki中解释是:Commons-FileUploadmakes it easy to add robust, high-performance, file upload capability to yourservlets and web applications.即给文件是与servlets原生支持的。关于怎么在struts2中使用只能通过官网docs来学习了。

 

官网给出的demo

http://commons.apache.org/proper/commons-fileupload/streaming.html

 

我在这里找到了Struts2的demo http://www.zuidaima.com/share/1833981853305856.htm

 

通过学习Struts2的demo我的问题解决了

以下贴出我自己的代码:

private static final long serialVersionUID = -7744625344830285257L;

	private File fileUpload;
	private String fileUploadContentType;
	private String fileUploadFileName;
	private String uploadDir;

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File file) {
		this.fileUpload = file;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public String execute() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		HttpServletRequest request=ServletActionContext.getRequest();
		
		String newFileName = null;
		long now = System.currentTimeMillis();
		// 得到保存上传文件的目录的真实路径
		File dir = new File(ServletActionContext.getServletContext()
				.getRealPath(uploadDir));
		// 如果该目录不存在,就创建
		if (!dir.exists()) {
			dir.mkdirs();
		}

	
		newFileName = now + ".jpg";

		InputStream is = null;
		OutputStream os = null;
		try {
			// 读取保存在临时目录下的上传文件,写入到新的文件中
			is = new FileInputStream(fileUpload);
			os = new FileOutputStream(new File(dir, newFileName));

			byte[] buf = new byte[1024];

			int len = -1;

			while ((len = is.read(buf)) != -1) {
				os.write(buf, 0, len);
			}
			//删除临时文件
			fileUpload.delete();
		} finally {
			is.close();
			os.close();
			if((new File(dir, newFileName)).exists()){
				request.setAttribute("upload.message", "上传图片成功!");
				String pic = newFileName;
				String dir2 = "img/";
				request.getRequestDispatcher("/jsp/upload.jsp?pic="+pic+"&dir="+dir2).forward(request, response);
			}else{
				request.setAttribute("upload.message", "上传图片失败!");
				request.getRequestDispatcher("/jsp/result.jsp").forward(request, response);
			}
			
		}
		return null;
	}

	public String display() {
		return "NONE";
	}





    
    
	
     
     
		
      
      /img
	
     
     

    
    




	
    
    



成功后的效果图:



值得注意的是文件上传之后扩展名变为temp,要在上传之前对文件扩展名进行判断。文件上传后的存储目录在struts.xml中有配置,文件信息是从jsp传递fileUpload到action中。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值