用Java,上传任何形式文件的代码:
<form id="swfImport" name="swfImport" method="post" ENCTYPE="multipart/form-data" target="_self">
<table><tr height="35">
<td align="left">路径:</td>
<td width="230"><s:file name="file" id="file" cssStyle="width:300px"></s:file></td>
</tr>
<tr height="35" align="left">
<td align="right" colspan="2"><input type="button" value="确定" onClick="save()"/><input type="button" value="取消" onClick="cancel()"/></td>
</tr>
</table>
</form>
function save(){
var filePath = document.getElementById("file").value;
var iIndex = filePath.length-4;
var szType = "";
//得到文件的后缀
if(iIndex > 0){
szType = filePath.substring(iIndex,filePath.length)
}
if((szType != ".swf") || filePath == ""){
alert("请选择swf文件!")
return;
}
swfImport.action = path+"/parkingGuideConfig!upload.action?type=" + szType;
swfImport.submit();
}
/**
* 上传swf文件到服务器
* java上传文件代码
* @return
*/
public String upload() throws Exception{
//获取文件上传路径
String root= getSession().getServletContext().getRealPath("/modules/guide/flex");
InputStream is=new FileInputStream(file);
File descFile=new File(root,"HelloWorld.swf");
OutputStream os=new FileOutputStream(descFile);
byte[] buffer=new byte[1024];
int length = 0;
while(-1!=(length=(is.read(buffer)))){
os.write(buffer, 0, length);
}
is.close();
os.close();
return "uploadSuccess";
}