问题:
页面只有2个上传的input 我只上传一张图片的时候是没问题的,在我本机的file目录下图片可以正常打开并且可以预览。
但是我同时上传2张图片的时候虽然上传成功了file目录下有2张图片,但是打开后都无法预览。
代码如下:
@Override
public String execute() throws Exception {
String tempname = "";//文件临时名称
String filedate = "";//文件日期(用于文件名称)
byte[] b = new byte[1024];
int len;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yymmddhhmmss");
for(int i =0;i<myFile.size();i++){
InputStream is = new FileInputStream(myFile.get(i));//获取上传文件
String path = ServletActionContext.getRequest().getRealPath("/file");
tempname = path+"\\"+sdf.format(date)+myFileFileName.get(i);
if(tempname.endsWith(".jpg")||tempname.endsWith(".bmp")){
imgPath = tempname;
System.out.println(imgPath);
}else{
videoPath = tempname;
System.out.println(videoPath);
}
if(imgPath!=null && !imgPath.equals("")){
OutputStream os = new FileOutputStream(imgPath);//IO操作文件复制到指定目录
while((len = is.read(b))!=-1){
os.write(b,0,len);
}
System.out.println("第一次-close");
os.close();
len=0;
}
if(videoPath!=null && !videoPath.equals("")){
OutputStream os2 = new FileOutputStream(videoPath);
while((len=is.read(b))!=-1){
os2.write(b,0,len);
}
os2.close();
}
is.close();
}
页面只有2个上传的input 我只上传一张图片的时候是没问题的,在我本机的file目录下图片可以正常打开并且可以预览。
但是我同时上传2张图片的时候虽然上传成功了file目录下有2张图片,但是打开后都无法预览。
代码如下:
@Override
public String execute() throws Exception {
String tempname = "";//文件临时名称
String filedate = "";//文件日期(用于文件名称)
byte[] b = new byte[1024];
int len;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yymmddhhmmss");
for(int i =0;i<myFile.size();i++){
InputStream is = new FileInputStream(myFile.get(i));//获取上传文件
String path = ServletActionContext.getRequest().getRealPath("/file");
tempname = path+"\\"+sdf.format(date)+myFileFileName.get(i);
if(tempname.endsWith(".jpg")||tempname.endsWith(".bmp")){
imgPath = tempname;
System.out.println(imgPath);
}else{
videoPath = tempname;
System.out.println(videoPath);
}
if(imgPath!=null && !imgPath.equals("")){
OutputStream os = new FileOutputStream(imgPath);//IO操作文件复制到指定目录
while((len = is.read(b))!=-1){
os.write(b,0,len);
}
System.out.println("第一次-close");
os.close();
len=0;
}
if(videoPath!=null && !videoPath.equals("")){
OutputStream os2 = new FileOutputStream(videoPath);
while((len=is.read(b))!=-1){
os2.write(b,0,len);
}
os2.close();
}
is.close();
}
解决方案 »
- jsp中下拉列表框问题
- 求一mysql正则表达式
- 请教个nutch的问题?
- 请问那块有Spring配置文件中属性详细的资料或书籍,多谢
- spring 异常 找了好久没找到
- [-求助-]简单的问题,在用myeclipse导入struts框架时,怎么设计才能把jar文件自动导入到WEB-INF/lib文件下呢?
- 十万火急!!为什么netbeens 无法安装在 xp操作系统上?
- 关于EJB部署的问题
- 刚学j2ee,只执行Context ctx = new InitialContext();测试代码就报错!详见文内错误信息
- 在servlet中的doGet或init或destroy中new File("myfile").createNewFile()
- Java发送传真
- struts2的Action如何设置pager.offset方法?
OutputStream os2 = new FileOutputStream(videoPath);
while((len=is.read(b))!=-1){
os2.write(b,0,len);
}
os2.close();
}
is.close(); 这段代码删除 只留一个上传的可以上传成功。不知道为什么,自己感觉应该是InputStream的问题。但又找不出那里有问题。
免分下载,
在上传文件的选择框中对文件进行过滤,只能选择图片,
图片上传完了之后直接在页面预览,
支持多文件无刷新批量上传,图片上传完毕自动加水印,
for(int i =0;i<myFile.size();i++){
InputStream is = new FileInputStream(myFile.get(i));//获取上传文件
String path = ServletActionContext.getServletContext().getRealPath("file");//得到绝对路径(用于上传)
HttpServletRequest request = ServletActionContext.getRequest();//获得request(用于获得相对路径)
times = sdf.format(date)+myFileFileName.get(i);//当前时间作为文件名称
relativepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/file/"+times;
System.out.println("相对路径=="+relativepath);
tempname = path+"\\"+times;
System.out.println("绝对路径=="+tempname);
if(tempname.endsWith(".jpg")||tempname.endsWith(".bmp")){
imgPath = "'"+relativepath+"'";//根据后缀名判断是图片路径
System.out.println("图片路径="+imgPath);
}else{
videoPath = "'"+relativepath+"'";
System.out.println("视频路径="+videoPath);
}
OutputStream os = new FileOutputStream(tempname);//IO操作文件复制到指定目录
while((len = is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
}