struts2上传文件的问题 刚开始传图片的时候没有什么问题,当传个几十张之后图片就不能传上去了,服务器不报任何错误和异常,只是会在tmp文件夹里面有临时文件存在。我在保存文件的那个方法的开始和结尾处 执行 system.out.printlin("----------------"),都打印出来了,望高手解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看一下你上传的最大数量是多少勒,真不知道为什么打印出system.out.printlin( "---------------- 应该是在你的代码里面有这些吧,去看看这里 我的图片名称是用的数据库的主键ID,主键是用uu.hex生产的,肯定是唯一的,比如我上传的图片是262K大小,他会在tmp临时文件夹里有一个262K大小的临时文件,虽然图片上传不上去,系统的其他功能都能正常使用,有时候上传个70到80张就不行了,有时候传十几张就不行了,必须重启tomcat才能继续使用 重启tomcat,不会是内存溢出了吧,捕获下OutOfMemoryError try { } catch(OutOfMemoryError e) { System.err.println/(e.getMessage()); } /** * 上传图片保存 * * @return */ public String addPicInfo() { try { System.out.println("--------------1"); PicInfo picInfo = new PicInfo(); picInfo.setCreatetime(new Date()); picInfo.setIntro(intro); picInfo.setPicname(picName);// PIC pic = new PIC(file);// String picPath = pic.save(getFileFileName(),webSiteId); String picPath = saveFile(); //System.out.println(picPath); picInfo.setPath(picPath); picInfo.setWebSiteId(Long.valueOf(webSiteId)); picService.save(picInfo); super.getRequest().setAttribute("picInfo", picInfo); System.out.println("--------------2"); return super.SUCCESS; } catch (Exception e) { e.printStackTrace(); return super.ERROR; } }private String saveFile(){ //System.out.println(uploadFileFileName); BufferedOutputStream out = null; BufferedInputStream in = null; FileOutputStream fos = null; FileInputStream fis = null; try { if(getFileFileName().indexOf(".")!=-1) fileFileName = getFileFileName().substring(getFileFileName().lastIndexOf(".")); String str = TimeFormater.formatNowTimetoString("yyyy-MM-dd"); String[] temp = str.split("-"); String tempPath1 =temp[0] + File.separator+temp[1] + File.separator + temp[2]+ File.separator; //Util.createPK()会返回一个唯一主键ID,我用他做文件名称 String subpath = tempPath1+ Util.createPK()+fileFileName; String vPath = Systemconfig.basePath+"ptl"+File.separator+webSiteId+File.separator+"upload"+File.separator +subpath; File folderFile = new File(Systemconfig.basePath+"ptl"+File.separator+webSiteId+File.separator+"upload"+File.separator +tempPath1); if(!folderFile.exists()) folderFile.mkdirs(); fos = new FileOutputStream(vPath); fis = new FileInputStream(getFile()); out = new BufferedOutputStream(fos); in = new BufferedInputStream(fis); int bytelength = (int)file.length(); byte [] buffer = new byte [bytelength]; while (in.read(buffer) > 0 ) { out.write(buffer); } in.close(); out.close(); fos.close(); fis.close(); subpath = subpath.replaceAll("\\\\", "/"); return subpath; } catch (Exception e) { try{ in.close(); out.close(); fos.close(); fis.close(); }catch(Exception ex){ ex.printStackTrace(); } e.printStackTrace(); return ""; } } 内存溢出的话会tomcat会报异常的,但是我其他功能都是正常的 大家帮我一起探讨一下看看有些什么可能性这个我也加过了 <filter> <filter-name> struts-cleanup </filter-name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> 求助 如何获取Struts2上传文件大小 在服务器端存储在线用户列表用什么方法好? MVC1与MVC2的区别 传值时值的类型转换问题 如何解决中Access的编码问题 求一个完整的JAVA调用存储过程的代码,请给的详细,急,必解贴 怎么安装jdk jsp怎么可以往用中文字段MYSQL数据库添加东西啊??? struts2如何解决同一action多次请求的数据缓存问题? JGraph图形如何在jsp页面中显示 两个jsp页面组合在一个页面里,怎样实现点击左边的超链右边的内容改变
system.out.printlin( "----------------
应该是在你的代码里面有这些吧,去看看这里
try { }
catch(OutOfMemoryError e) {
System.err.println/(e.getMessage());
}
* 上传图片保存
*
* @return
*/
public String addPicInfo() {
try {
System.out.println("--------------1");
PicInfo picInfo = new PicInfo();
picInfo.setCreatetime(new Date());
picInfo.setIntro(intro);
picInfo.setPicname(picName);// PIC pic = new PIC(file);
// String picPath = pic.save(getFileFileName(),webSiteId);
String picPath = saveFile();
//System.out.println(picPath);
picInfo.setPath(picPath);
picInfo.setWebSiteId(Long.valueOf(webSiteId));
picService.save(picInfo);
super.getRequest().setAttribute("picInfo", picInfo);
System.out.println("--------------2");
return super.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return super.ERROR;
}
}
private String saveFile(){
//System.out.println(uploadFileFileName);
BufferedOutputStream out = null;
BufferedInputStream in = null;
FileOutputStream fos = null;
FileInputStream fis = null;
try {
if(getFileFileName().indexOf(".")!=-1)
fileFileName = getFileFileName().substring(getFileFileName().lastIndexOf("."));
String str = TimeFormater.formatNowTimetoString("yyyy-MM-dd");
String[] temp = str.split("-");
String tempPath1 =temp[0] + File.separator+temp[1] + File.separator + temp[2]+ File.separator;
//Util.createPK()会返回一个唯一主键ID,我用他做文件名称
String subpath = tempPath1+ Util.createPK()+fileFileName;
String vPath = Systemconfig.basePath+"ptl"+File.separator+webSiteId+File.separator+"upload"+File.separator +subpath;
File folderFile = new File(Systemconfig.basePath+"ptl"+File.separator+webSiteId+File.separator+"upload"+File.separator +tempPath1);
if(!folderFile.exists())
folderFile.mkdirs();
fos = new FileOutputStream(vPath);
fis = new FileInputStream(getFile());
out = new BufferedOutputStream(fos);
in = new BufferedInputStream(fis);
int bytelength = (int)file.length();
byte [] buffer = new byte [bytelength];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
in.close();
out.close();
fos.close();
fis.close();
subpath = subpath.replaceAll("\\\\", "/");
return subpath;
} catch (Exception e) {
try{
in.close();
out.close();
fos.close();
fis.close();
}catch(Exception ex){
ex.printStackTrace();
}
e.printStackTrace();
return "";
}
}
这个我也加过了
<filter>
<filter-name> struts-cleanup </filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>