我前几天做了一个使用Struts FormFile上传的东西,但是在使用中发现上传的速度非常慢,选择一个文件上传,点击以后在后台处理的Action方法里面加断点,发现从点击上传到文件全部传送至服务器端这个过程非常慢,一个2M的文件在局域网传送居然要50秒时间。同时我也做了个下载,速度非常的快,所以我怀疑是将request中的文件流写入FormFile的时候出的问题,我找了好久都没能提高上传速度 ,把代码贴在这里希望大家能帮我想想办法。Upload.jsp:
代码
<html:form action="UploadAction.do" method="POST" enctype="multipart/form-data">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#C8E8F0" id="advertServer">
<td align="right">选择文件</td>
<td height=30 bgcolor="#F7FBFF">
<html:file property="theFile" styleId="theFile" styleClass="textbox" size="50" >
</td>
</tr>
</table>
</html:form>
UpLoadForm.java: 代码
public class UpLoadForm extends ValidatorForm { private FormFile theFile; setter and getter method here..
}UpLoadAcion.java 代码
public class UpLoadAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm argForm,HttpServletRequest request, HttpServletResponse response) {
UpLoadForm form = (UpLoadForm)argForm;
FormFile uploadFile = (FormFile) form.getTheFile();
ServletContext context = getServlet().getServletContext();
String filePath = context.getRealPath("/");// 取当前系统路径
stream = formFile.getInputStream();// 把文件读入
bos = new FileOutputStream(fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {bos.write(buffer, 0, bytesRead);// 将文件写入服务器
mapping.findForward("success");
}
}我已经把多余部分去掉了,和上传有关的就这么多代码,我就不知道为什么会那么慢,郁闷 顺便问一个问题,大家做上传的时候应该是不允许那个Text框里面随便输入东西的,否则不好控制,我想知道大家是怎么做的,我现在只是做了一个onchange 的事件,控制了一下上传文件的后缀名,但是这个是远远不够的,请大家指点,先谢谢了!
代码
<html:form action="UploadAction.do" method="POST" enctype="multipart/form-data">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#C8E8F0" id="advertServer">
<td align="right">选择文件</td>
<td height=30 bgcolor="#F7FBFF">
<html:file property="theFile" styleId="theFile" styleClass="textbox" size="50" >
</td>
</tr>
</table>
</html:form>
UpLoadForm.java: 代码
public class UpLoadForm extends ValidatorForm { private FormFile theFile; setter and getter method here..
}UpLoadAcion.java 代码
public class UpLoadAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm argForm,HttpServletRequest request, HttpServletResponse response) {
UpLoadForm form = (UpLoadForm)argForm;
FormFile uploadFile = (FormFile) form.getTheFile();
ServletContext context = getServlet().getServletContext();
String filePath = context.getRealPath("/");// 取当前系统路径
stream = formFile.getInputStream();// 把文件读入
bos = new FileOutputStream(fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {bos.write(buffer, 0, bytesRead);// 将文件写入服务器
mapping.findForward("success");
}
}我已经把多余部分去掉了,和上传有关的就这么多代码,我就不知道为什么会那么慢,郁闷 顺便问一个问题,大家做上传的时候应该是不允许那个Text框里面随便输入东西的,否则不好控制,我想知道大家是怎么做的,我现在只是做了一个onchange 的事件,控制了一下上传文件的后缀名,但是这个是远远不够的,请大家指点,先谢谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货