关于后台处理上传图片和显示图片的简单介绍(亲测)

1.首先有一个包装类

package com.marvin.book.pojo;

import java.io.File;

public class BookFile {
	private File image;
	private String imageFileName;
	private String imageContentType;
	
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public String getImageContentType() {
		return imageContentType;
	}
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
}



2.这个包装类用于保存图片和自动解析图片的文件名(将这个文件作为action的全局变量就好了,或者写在baseAction中)

前台上传图片的name是这个类的一个属性  name = 后台变量名.image
下面是前台的举例(extjs)

 items:[{
	        	xtype:'filefield',
				name:'photos.image',
		       	labelWidth:60,
		       	msgTarget: 'side',
		       	anchor: '100%',
		       	buttonText: '选择照片',
		       	fieldLabel : '上传图片'
			}

java前台更简单哟,直接加上一个file将name命名为 name = 后台变量名.image 就好了


3.后台承接到对象就直接可以存储了,下面是获取存储位置的文件路径的方法

servletContext.getRealPath("upload");
String savePath = realPath + "/" + uploadFileName


4.不过本人喜欢写一个上传的工具类,用这个类处理每一次上传,然后上传的路径用spring直接注入进来就好了呀.....

package com.marvin.book.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.junit.Test;

import com.marvin.book.pojo.BookFile;

public class FileUploadUtil {
	private String filePath = null;
	
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	//一个方法一个功能
	public String getExt(String fileName){
		return fileName.substring(fileName.lastIndexOf(".")+1);
	}
	
	public String createFileName(String fileName){
		return UUID.randomUUID().toString()+"."+getExt(fileName);
	}
	
	//删除文件
	public void delete(String fileName){
		new File(fileName).delete();
	}
	
	//进行另一个文件位置的存储
	public String uploadFile(File file, String fileName, String path){
		if(path != null){
			filePath = path;
		}
		return this.uploadFile(file, fileName);
	}
	
	public String[] bankImage(String path){
		File file = new File(path);
		
		return file.list(new FilenameFilter() {
			
			public boolean accept(File dir, String name) {
				return name.endsWith("gif");
			}
		});
	}
	
	//上传文件返回新的文件名
	public String uploadFile(File file, String fileName){
		//生成新的文件名
		String newName = createFileName(fileName);
		//文件上传
		InputStream input = null;
		OutputStream output = null;
		try { 
			input = new FileInputStream(file); 
			output = new FileOutputStream(filePath + "/"+newName);
			byte[] b =new byte[1024];
			int length = 0;
			while((length=input.read(b)) != -1){
				output.write(b, 0, length);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			try {
				input.close();
			} catch (Exception e) {
				throw new RuntimeException(e);
			} finally{ 
				try {
					output.close();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			
		}
		
		return newName;
	}
}

小注:获取图片实际路径的方法有很多,自己可以去网上进行搜索,比如: ServletContextAware(Action内部即可直接使用ServletContext对象)


还有需要注意:

Struts2配置文件中的struts.multipart.saveDir起什么作用? 
原来初步感觉这个文件夹就服务端保存上传文件的文件夹,不过根本不是这么回事! 
这个文件夹只是用来保存上传文件的“临时路径”,文件上传完毕后就会从此路径移除。 
如果不配置将使用默认的 javax.servlet.context.tempdir 来保存临时文件。 
所以这个目录设不设无所谓,尼玛又误导我一次!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值