代码如下:
smartupload04.htm:
<html>
<head>
<title>smartupload</title>
</head>
<body>
<form action="smartupload04.jsp" method="post" enctype="multipart/form-data">
文件名称:<input type="text" name="uname"><br>
上传的图片:<input type="file" name="pic"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
-------分割线 -------------
smartupload04.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:useBean id="smartupload" class="org.lxh.smart.SmartUpload"/>
<html>
<head>
<title>smartupload</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK") ;
smartupload.initialize(pageContext) ; // 初始化上传
smartupload.upload() ; // 准备上传
String name = smartupload.getRequest().getParameter("uname") ;
name = name + "." + smartupload.getFiles().getFile(0).getFileExt() ;
String fileName = this.getServletContext().getRealPath("/") + "upload/" + name ;
smartupload.getFiles().getFile(0).saveAs(fileName) ;
%>
<img src="upload/<%=name%>" width="300" height="200">
</body>
</html>