大家好,本人是个技术菜鸟,刚接触项目,需同时上传视频和图片,代码如下:
public String upload(ActionContext ctx) throws Exception{
 String fileName = DateUtils.getSystemTime() ;
 String outPath = getBasePath() + "/" + fileName + "mp4";
FileOutputStream fos = null;
FileInputStream fis = null;
try{
fos = new FileOutputStream(outPath);
fis = new FileInputStream(getSS());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if (fos!=null){
fos.close();
}
if (fis != null){
fis.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
src = basePath + "//" + fileName;
while (src.startsWith("/")){
src = src.replaceFirst("/", "");
}
ctx.put("sUrl", src);
return "upload";
}在上面的代码中,upload是调用的一个方法,视频和图片都可以上传,只是无论什么文件上传之后全部显示的是mp4格式,其实我也知道是因为我在String outPath = getBasePath() + "/" + fileName + "mp4";加的是mp4才导致这样,但是现在我想上传后的文件能够保持原来的格式,也就自动识别文件格式,比如上传jpg就显示jpg,上传wmv就显示wmv。我试过其他方法,没有找到合适的,希望熟悉的大侠帮忙解决。
说的有点罗嗦了,只是为了让大家明白我的意思,先谢谢大家。