文件上传
2020-12-18 23:32
标签:val 工厂 port 创建目录 cat dog gbk cti catch 利用uploadservlet 代码 利用layui的上传 1.现因文件 上传文件 文件上传 标签:val 工厂 port 创建目录 cat dog gbk cti catch 原文地址:https://www.cnblogs.com/cjw554551/p/14130661.htmlpackage com.oracle.web.product;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.IOUtils;
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 创建磁盘文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 创建文件上传核心对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析request获得文件项对象集合
List
"css/layui.css">
//js代码
layui.use(‘upload‘, function(){
var upload = layui.upload;
//执行实例
var uploadInst = upload.render({
elem: ‘#test1‘ //绑定元素
,url: ‘${pageContext.request.contextPath }/UploadServlet‘ //上传接口
,done: function(res){
//上传完毕回调
alert(res.path);
$("#pimg").val(res.path);
$("#imge").prop("src",res.path);
}
,error: function(){
//请求异常回调
}
});
});
下一篇:WSGI web框架的实现