struts2 上传文件主要代码
来源:互联网 发布:proc sql定义变量长度 编辑:程序博客网 时间:2024/06/01 10:31
public File file; //与页面上的 input file 标签的name 一样
public String name;
/**
* 上传文件公共方法
* @param url 地址
* @param ext 后缀名
* @return 上传成功
*/
public boolean file(String url,String ext) {
response = getResponse();
InputStream input = null;
long time = System.currentTimeMillis();
OutputStream os = null;
try {
System.out.println(name);
SystemParameter systemParameter = (SystemParameter) getRequest()
.getSession().getServletContext()
.getAttribute("systemParameter");
String path = systemParameter.getFilePath() + url ;
String houzui = name.substring(name.lastIndexOf("("), name.length());
if (houzui.equals("(空白)")) {
name=name.substring(0, name.indexOf("("));
}
if (name.equals("流程图")) {
name="liuchen";
}
// 检查文件是否存在
File f = new File(path, name+ ext);
if (f.exists()) {
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
String newName = path + name + "old_" + time
+ ext;
System.out.println(newName);
boolean is = f.renameTo(new File(newName));
if (is == false) {
f.renameTo(new File(path + name + ext));
return false;
} else {
// 如果存在,在读一次改过文件的名字,如果新的名字不存在,就提示上传失败
File file = new File(newName);
if (!file.exists()) {
return false;
}
}
}
input = new FileInputStream(file);
os = new FileOutputStream(f);
byte[] by = new byte[1024];
int length = input.read(by);
while (length != -1) {
os.write(by, 0, length);
length = input.read(by);
}
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
if (os != null)
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- struts2 上传文件主要代码
- struts2 上传文件主要代码
- Struts2上传文件代码
- PHP文件上传主要代码讲解
- struts2实现文件上传 代码
- struts2示例代码-多个文件上传
- Struts2多文件上传 Action关键代码
- struts2上传文件(三) struts2上传文件
- Struts2+Jquery.uploadify实现的文件上传特效示例代码
- struts2上传多个文件,下载 配制!代码
- Struts2 使用List同时上传文件 关键代码
- Struts2:实现多个文件同时上传代码
- Struts2文件上传配置教程Demo代码下载
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- 将h.264视频流封装成flv格式文件(二.开始动手)
- 多校第一场——hdu4605——离线
- ListView点击事件失效
- ExtJS梦想之旅(四)--Panel的使用
- Android基础之Launcher开发
- struts2 上传文件主要代码
- ARM9学习笔记之——SDRAM实验
- CUDA从入门到精通(五):线程并行
- 多校联赛第一场
- 按照RFC3984协议实现H264视频RTP打包(附源代码)
- 给函数增加Debug头 - windows平台的普通打印调试处理
- python字符串与数字类型转化
- 数据类型转换
- H264和MPEG4起始码(startcode)