public class UploadAction extends BaseAction{
private static final long serialVersionUID = 1L;
private File file;
private String fileName;
private String fileType;
@Action(value = "/backend/upload/uploadvideo")
public void uploadvideo() {
JSONObject json = new JSONObject();
try {
String filePath = CommonUpload.upLoad(fileName,fileType,file);
json.accumulate("success", true);
json.accumulate("filePath", filePath);
json.accumulate("fileName", videoFileFileName);
} catch (IOException e) {
json.accumulate("success", false);
json.accumulate("errorMsg", "上传失败! 原因为"+e.getMessage());
}
AjaxResponse.responseOut(json);
}
}
public class CommonUpload {
public static final String UPLOAD_ROOT = "backend/info/upload";
public static final int BUFFER_SIZE = 1024;
public static String upLoad(String fileName, String pictureType, File file) throws IOException {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd/HH");
String ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
String type;
if(ext.equals("jpg")||ext.equals("jpeg")||ext.equals("png")||ext.equals("gif")||ext.equals("bmp")){
type="image";
}else if(ext.equals("mp4")||ext.equals("avi")){
type="video";
}else{
type="brochure";
}
String pathdir = "/" + UPLOAD_ROOT + "/" + pictureType + "/" +"/" + type + "/" + dateformat.format(new Date());
String realpathdir = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(pathdir);
File savedir = new File(realpathdir);
if (!savedir.exists()) {
savedir.mkdirs();
}
String filename = UUID.randomUUID().toString() + "." + ext;
String path = pathdir + "/" + filename;
FileOutputStream fileoutstream = new FileOutputStream(new File(realpathdir, filename));
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fileoutstream.write(buffer, 0, len);
}
fileoutstream.close();
fis.close();
return path;
}
}