代码
- upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="fileName"> <br/>
<input type="submit" name ="upload" value="上传">
</form>
</body>
</html>
- UploadwithServlet.java
package henu.file;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class UploadwithServlet extends HttpServlet {
// 声明servletconfig对象,作为initialize()方法的参数
ServletConfig servletconfig;
public UploadwithServlet() {
super();
}
// 初始化servletconfig对象
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.servletconfig = config;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("进来了!");
// 1.实例化一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 2.初始化SmartUpolad对象
try {
su.initialize(servletconfig, request, response);
} catch (ServletException e1) {
e1.printStackTrace();
}
// 3.设置文件上传的 限制
su.setAllowedFilesList("doc,docx,txt");
// 单个文件最大字节数
su.setMaxFileSize(3 * 1024 * 1024);
// 文件总最大字节数
su.setTotalMaxFileSize(12 * 1024 * 1024);
// 4.使用upload上传
try {
su.upload();
} catch (ServletException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} catch (SmartUploadException e2) {
e2.printStackTrace();
}
// 5.文件保存
Date curDate = new Date();
long d = curDate.getTime();// long型
// 创建request对象
Request req = su.getRequest();
// 获取上传文件
// 获取全部上传文件
Files files = su.getFiles();
// 获取指定位置的文件
File file = files.getFile(0);
// 获取文件扩展名
String extFile = file.getFileExt();
String mainFile = String.valueOf(d);
System.out.println("test");
String filename = "/upload02/" + mainFile + "." + extFile;
System.out.println(filename);
try {
file.saveAs(filename);
} catch (IOException e1) {
e1.printStackTrace();
} catch (SmartUploadException e1) {
e1.printStackTrace();
}
}
}
- web.xml
<!-- 文件上传 -->
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>henu.file.UploadwithServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
编译文件位置
1.idea的文件传输渠道一般是在out文件夹
2. eclipse的默认web项目路径是在:
工作空间的metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps里面
如:
“D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ch8\upload\1652886156910.jpeg”
报错
- 若不存在所指定的文件夹,应按上述路径自己创建一个。(我的idea时而可以自己创建时而又报这个错误,求指教)