1. 程式人生 > >java上傳檔案時tomcat崩潰

java上傳檔案時tomcat崩潰

在專案中,我用到的tomcat是5.5的,在linux上跑。最近發現在上傳檔案時,如果連續上傳10個左右,就會出現tomcat崩潰的情況。JVM的記憶體我已經設定成為512M的了。現將程式碼分別貼出,請各位看一看:(是SSH架構的)

jsp:

<form action="tArenamessage.do" name="tArenamemessageForm" id="tArenamemessageForm" method="POST" onSubmit="return checkForm('tArenamemessageForm');" enctype="multipart/form-data" >
...
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="file" name="file3" />
....
/form>

===============================

struts.xml:

<form-bean name="TArenamemessageForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="name" type="java.lang.String" />
...
<form-property name="file1" type="org.apache.struts.upload.FormFile" />
<form-property name="file2" type="org.apache.struts.upload.FormFile" />
<form-property name="file3" type="org.apache.struts.upload.FormFile" />
</form>
<!-- 場館管理模組 -->
<action path="/tArenamessage" scope="request" type="org.springframework.web.struts.DelegatingActionProxy" validate="false" name="TArenamemessageForm" >
<forward name="service" path="/../WEB-INF/jsp/admin/jsp/tArenamessage.jsp"> </forward>
</action>

====================================

aciton:

//儲存
public boolean setBeanValueByDynaActionForm(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
this.myExecute(mapping, form, request, response);
DynaActionForm hzForm = (DynaActionForm) form;
...
FormFile localfile = null;
//外景照片
localfile = (FormFile)hzForm.get("file1");
if(localfile!=null) {this.upload(localfile, request, 6L);}

localfile = (FormFile)hzForm.get("file2");
if(localfile!=null) {this.upload(localfile, request, 6L);}

localfile = (FormFile)hzForm.get("file3");
if(localfile!=null) {this.upload(localfile, request, 6L);}

...
}
//上傳檔案
private void upload(FormFile localfile,HttpServletRequest request,long flag){
String finalFileName="";
if(localfile!=null) {
finalFileName = Upload.uploadAttachment( localfile , request);//上傳檔案
}
if(finalFileName != null && !finalFileName.equals("")){
uploadFile(localfile,request,finalFileName,flag);//儲存到附件表
}
}

上傳檔案的類:
public class Upload {
public static String uploadAttachment( FormFile file , HttpServletRequest request){
if( SystemGlobal.uploadAccess[0].toString().indexOf(file.getContentType())>=0 && file.getFileSize() <= ( 2048 * (Integer.parseInt( SystemGlobal.uploadAccess[1].toString()) ))){ //是圖片並且小於?k
String path = SystemGlobal.webDir(request) + SystemGlobal.uploadAccess[2] + System.getProperty("file.separator") + StringUtil.date2String() + System.getProperty("file.separator");
File dir = new File(path);
if(!dir.exists()){dir.mkdir();}
String filename = "";
if(!file.getFileName().equals("")){
try{
filename = StringUtil.time2String() + StringUtil.ramdom(4)+ "." + file.getFileName().substring(file.getFileName().length()-3) ;
InputStream stream = file.getInputStream();
FileOutputStream output = new FileOutputStream( path + filename );
int len = -1;
byte[] buf = new byte[102400];
while((len = stream.read(buf)) != -1){
output.write(buf,0,len);
}
output.close();
stream.close();
}catch(IOException e){Log.error( Upload.class , "upload write stream ha error:" + e);}
}
return filename;
}
return "";
}

==============================
tareamanage.hbm.xml 一對多,一個場館可以有多個圖片

<set name="TUploadfiles" inverse="false" cascade="save-update" fetch="subselect" lazy="false">
<cache usage="read-write"/>
<key>
<column name="OBJID" length="50" />
</key>
<one-to-many class="com.webaorta.comm.pojo.TUploadfile" />
</set>

====================================
大致上就是這些了,程式碼裡是否存在不合理的地方而導致什麼原因從而令到tomcat死掉了呢?tomcat裡的logs也沒有一點報錯的資訊可以看的。


後來我直接將其轉換為open session in view模式,奇怪的是我在windows下可以執行(開發機),但在linux下就報session已經關閉這個錯誤的了。這又是為什麼呢?(伺服器是linux系統).

請各位看一看吧,如果搞不好這個可能會丟飯碗的。謝謝大家了。