ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [转载]用JSP/Servlets实现Web方式的文件上传

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

原创 Linux操作系统 作者:dinner1007 时间:2019-05-08 18:27:04 0 删除 编辑
用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
-->



2. JSP File
<%@ page language="java" import="com.jspsmart.upload.*,
java.sql.*,java.util.* "%>
<%-- use SmartUpload bean --%>
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博客 ” ,链接:https://blog.itpub.net/374079/viewspace-131129/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-08-23

  • 博文量
    1714
  • 访问量
    1477684