Java上传文件

用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";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值