文件上传实际中需要注意的问题

来源:互联网 发布:tcl java待遇怎么样 编辑:程序博客网 时间:2024/06/05 08:02

一,文件上传的时候大小的限制

struts-config.xml里面加个控制器

<controller maxFileSize="3M"></controller>

然后在formbean里面要判断一下,因为当你上传的东西超过了大小的时候,不让请求进入到action中,防止空指针异常

(因为当上传的文件超过了大小的时候,actionSerlvet里面的requestProcessor处理器不会去填充被内部包装的一个request里面的map里面的属性,可以理解为不会填充formbean里面的内容)

formbean中下个validate方法:

public ActionErrors validate(ActionMapping mapping,

HttpServletRequest request) {

if(file!=null){

return null;

}else{

//这里是为了的测试,所以加了个错误

//实际中不是这样加的,要有相应的message的可以在资源文件中配置

ActionErrors errors = new ActionErrors();

ActionMessage message = new ActionMessage("key");

errors.add("prop", message);

return errors;

}

}

配置了之后,文件上传的大小限制就被控制了

二,文件名的控制,上传的文件不能是文件重名的冲突

为了防止上传到服务器上的文件重名的情况,我们使用世界上唯一的UUID来处理文件名,把原来的文件名改成UUID的文件名来存储

//处理文件上传的目录和文件名

(这里是从action传过来的文件名字,服务器上的真实路径,this.getServlet().getServletContext().getRealPath("....")

public static String getSavePath(String fileName,String realPath){

//把文件名改成唯一的文件名 用UUID

UUID uuid = UUID.randomUUID();

String extraName = fileName.substring(fileName.lastIndexOf("."));

String uniqueFileName = uuid+extraName;

//文件的存储目录

Calendar calendar = new GregorianCalendar();

//===注意这里的盘符啊,在java中有盘符的绝对路径的时候要\\,因为编译器不认识\ 要转移\\

//==相对路径就用这个/

String path = realPath+"\\"+calendar.get(Calendar.YEAR)+"\\"+calendar.get(Calendar.MONTH)+

"\\"+calendar.get(Calendar.DATE);

//不要忘了要创建不存在目录的时候

File file = new File(path);

if(!file.exists()){

file.mkdirs();

}

return path+"\\"+uniqueFileName;

}

三,文件存储目录的问题,存入到web服务器上的路径要能够提高效率,不能把所有的文件放到一个文件夹中,时间长了,很多文件在同一个文件夹中,去找这个文件的时候要消耗很多的时间,效率非常低,所以在文件上传的时候要把目录分级

代码放在上面那个例子上面

四,为了能够和客户进行很好的交互,应该让可以指定上传的路径,所以可以把文件路径写在配置文件中

1,把配置文件写在类文件下

为了提高下效率,要使用单例模式,因为每次上传都要读配置文件,效率很低

public class ConfigPath {

//这里是配置文件中的key

public static final String propKey = "hwt.MultipartUpload.relativePath";

private static Properties prop = null ;

private ConfigPath(){}

static{

prop = new Properties();

//配置文件要放在类文件目录下面

InputStream inStream = ConfigPath.class.getResourceAsStream("/configPath.properties");

try {

prop.load(inStream);

catch (IOException e) {

e.printStackTrace();

}

}

//返回配置文件

public static Properties getProp(){

return prop;

}

//处理文件上传的目录和文件名

public static String getSavePath(String fileName,String realPath){

//把文件名改成唯一的文件名 用UUID

UUID uuid = UUID.randomUUID();

String extraName = fileName.substring(fileName.lastIndexOf("."));

String uniqueFileName = uuid+extraName;

//文件的存储目录

Calendar calendar = new GregorianCalendar();

//===注意这里的盘符啊,在java中有盘符的绝对路径的时候要\\,因为编译器不认识\ 要转移\\

//==相对路径就用这个/

String path = realPath+"\\"+calendar.get(Calendar.YEAR)+"\\"+calendar.get(Calendar.MONTH)+

"\\"+calendar.get(Calendar.DATE);

//不要忘了要创建不存在目录的时候

File file = new File(path);

if(!file.exists()){

file.mkdirs();

}

return path+"\\"+uniqueFileName;

}

}

2,把配置文件放在webroot下面

使用servletConetext的监听器或者strutsplugin来实现(用strutsplugin的时候,不要在另外写配置文件了,因为可以在struts-config.xml里面配置<set-property>),在服务启动的时候就读一下配置文件,然后把properties这个对象放入到application的作用域中

public class ConfigServletListener implements ServletContextListener {

//摧毁

public void contextDestroyed(ServletContextEvent arg0) {

}

//初始化

public void contextInitialized(ServletContextEvent arg0) {

Properties prop = new Properties();

String path = arg0.getServletContext().getRealPath("/config.properties");

try {

FileInputStream fileInputStream = new FileInputStream(path);

prop.load(fileInputStream);

fileInputStream.close();

catch (FileNotFoundException e) {

e.printStackTrace();

catch (IOException e) {

e.printStackTrace();

}

arg0.getServletContext().setAttribute("prop", prop);

}

}

原创粉丝点击