package cn.ubiton.com.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class Upload {
/**
* 普通批量上传文件方法
*
* @param fa
* 文件数组
* @param fna
* 文件名称数组
* @return 上传文件的路径字符串
*/
public String commUpload(File[] fa, String[] fna) {
String paths = "";
ActionContext ac = ActionContext.getContext();
ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
String rp = sc.getRealPath("/"), folder = "", newNm = "", ext = "";
// String rp = "/data/web/static/", folder = "", newName = "";
File dir = null, destFile = null;
InputStream is = null;
OutputStream os = null;
for(int i = 0; i < fa.length; ++i) {
ext = fna[i].substring(fna[i].lastIndexOf(".")).toLowerCase();
if(".jpg.png.bmp.gif".indexOf(ext) != -1) {
folder = newPath(1, rp);
} else if(".flv".equals(ext)) {
folder = newPath(2, rp);
} else {
folder = newPath(3, rp);
}
newNm = newName(fna[i]);
dir = new File(folder);
if(!dir.exists()) {
dir.mkdirs();
}
destFile = new File(dir, newNm);
try {
is = new FileInputStream(fa[i]);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
paths += dir + newNm +",";
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
paths = paths.substring(0, paths.length()-1);
return paths;
}
/**
* 单个上传文件方法
*
* @param fa
* 文件
* @param fna
* 文件名称
* @return 上传文件的路径
*/
public String singleUpload(File fa, String fna) {
String path = "";
ActionContext ac = ActionContext.getContext();
ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
String rp = sc.getRealPath("/"), folder = "", newNm = "", ext = "";
// String rp = "/data/web/static/", folder = "", newName = "";
File dir = null, destFile = null;
InputStream is = null;
OutputStream os = null;
ext = fna.substring(fna.lastIndexOf(".")).toLowerCase();
if(".jpg.png.bmp.gif".indexOf(ext) != -1) {
folder = newPath(1, rp);
} else if(".flv".equals(ext)) {
folder = newPath(2, rp);
} else {
folder = newPath(3, rp);
}
newNm = newName(fna);
dir = new File(folder);
if(!dir.exists()) {
dir.mkdirs();
}
destFile = new File(dir, newNm);
try {
is = new FileInputStream(fa);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
path = dir + newNm;
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}
return path;
}
/**
* 上传文件路径
*
* @param fType
* 文件类型标识,1为图片,2为视频,3为其它文件
* @param rp
* 上传文件路径
* @return 上传文件的路径
*/
public String newPath(int fType, String rp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
String dts = sdf.format(new java.util.Date());
switch(fType) {
case 1 :
rp += "files/img/" + dts + "/l/";
break;
case 2 :
rp += "files/vdo/" + dts + "/";
break;
case 3 :
rp += "files/oth/" + dts + "/";
break;
default :
rp += "files/oth/" + dts + "/";
break;
}
return rp;
}
/**
* 重命名上传文件
*
* @param srcName
* 上传文件的文件名
* @return 重命名后的文件名
*/
public String newName(String srcName) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String ext = srcName.substring(srcName.lastIndexOf(".")), dt = sdf.format(new java.util.Date()), rd = Math.round(Math.random() * 900) + 100 + "";
return dt + rd + ext;
}
}
henry728
- 粉丝: 1
- 资源: 4
最新资源
- FM9919E:高性能副边同步整流驱动芯片的技术解析
- (源码)基于Spring Security和Redis的单点登录系统.zip
- (源码)基于Arduino实现的CRC硬件校验系统.zip
- 半桥电路的开环仿真PSIM
- (源码)基于C++的RucBase数据库管理系统.zip
- 美国华盛顿州电动汽车保有量数据集(21W+记录)CSV+XML+JSON+RDF格式
- 低功耗原边反馈开关电源芯片TC2526HA/TC2526HB的技术解析
- (源码)基于PyTorch框架的图像识别系统.zip
- Java项目:图书管理系统(基于Java+Springboot+Maven+MyBatisPlus+Vue+Mysql)
- 使用C语言实现字符串逆序输出实现方式.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页