@Override
public String UploadFile(HttpServletRequest request)
throws IllegalStateException, IOException
{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
// 获取服务器上下文路径
String path = Global.getFtpDirectory() + "/" + Global.getSw();
// 创建文件存放路径
File folder = new File(path);
if (!folder.exists())
{
folder.mkdirs();
}
// 获取上传的文件
MultipartFile multiFile = multipartRequest.getFile("file");
// 获得文件全名
String fname = multiFile.getOriginalFilename();
// 创建上传文件全信息为查找看是否能读到本次上传文件
File isfile = new File(path + "/" + fname);
long sizeNum = multiFile.getSize();
String softVersion = fname.split("v")[1].substring(0, 7);
String fileNames = path + "/" + fname;
File file = new File(fileNames);
// 如果文件不存在写文件到服务器
multiFile.transferTo(file);
return Global.SUCCESS;
}
上面是java代码,JSP代码为:
<body>
<form action="saveDataFile" id="soft_add" name="data_add" method="post" enctype="multipart/form-data">
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top: 20px;margin-bottom: 15px;">
<tr><td align="center"><font size="3"><b> 数 据 文 件 入 库 </b></font></td></tr>
<tr><td align="center"><FONT SIZE="" COLOR="red">*</FONT> <font size="2">为必填项</font></td></tr>
</table>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="borderquan">
<tr>
<td width="20%" height="50" align="center" class="borderbottomright">数据文件</td>
<td width="80%" class="borderbottom"><label>
<input type="file" name="dataFile" id="dataFile" style="width: 310px;" οnblur="checkDataFile();"/>
<FONT SIZE="" COLOR="red" style="font-size: 12px">* </FONT>
<span id="staError" style="font-size: 9pt; font-family: 黑体; color: red"></span>
<FONT SIZE="" COLOR="red" style="font-size: 12px"><br />文件名格式如:2_v110.xml 下划线前是基站标识,下划线后是数据版本</FONT>
</label></td>
</tr>
<tr>
<td width="20%" height="30" align="center" class="borderbottomright">描述</td>
<td width="80%" class="borderbottom"><label>
<textarea name="descInfo" style="width: 310px;" rows="5"></textarea>
</label></td>
</tr>
<tr>
<td height="30" colspan="2" align="center">
<input type="button" name="save" class="myBtn" value="保 存" οnclick="submitForm();" />
<input type="button" name="backtrack" class="myBtn" value="返 回" οnclick="goback();"/>
</td>
</tr>
</table>
<table width="99%" cellpadding="0" cellspacing="0" align="center" style="margin-top: 20px;">
<tr>
<td align="center"><font color="red" style="font-size: 15px;">
<c:if test="${result!=null }">
${result }
</c:if>
</font>
</td>
</tr>
</table>
</form>
</body>