[转载]用JSP/Servlets实现Web方式的文件上传

博客给出了JSP文件的代码示例,包含初始化、设置文件上传限制、上传文件及保存文件到磁盘等操作,若上传失败会给出相应提示信息,还提及有Servlet文件可自行完成。
用JSP/Servlets实现Web方式的文件上传
Use JSP and Servlet to Upload file to server is becoming a
common task now.
Below is a simple example use the jspsmart free component which
implements it.
You can download the free package from JSPSMART

1. Html File




action="uploadImage.jsp" ENCTYPE="multipart/form-data"
target=_self>

广告图片上载
广告图片:name="adsImage" size="20">


   


<!--
close this window
--&gt



2. JSP File
java.sql.*,java.util.* "%>

class="com.jspsmart.upload.SmartUpload" />


//Initialization
mySmartUpload.init(config);
mySmartUpload.service(request,response);

//Set Restriction
mySmartUpload.setAllowedFilesList("gif,bmp,jpeg,jpg");
mySmartUpload.setTotalMaxFileSize(51200);



// Upload
try{
mySmartUpload.upload();
}
catch(Exception e){
out.println(" Upload File Fail!
"JavaScript:window.history.back();">back
");
out.println("Notes:");

out.println("
  • You must set correct File
    Name.
");
out.println("
  • You file size must be less than
    50K.
");
out.println("
  • You can only upload .Gif .jpeg .jpg and
    .bmp files.
");

return;

}
//save file to disk
mySmartUpload.getFiles().getFile(0).saveAs("/" +
"FileName");

 

%>

3. Servlet File (you can do it yourself)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-131129/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-131129/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值