如何用jsp上传图片到指定文件夹

发布网友

我来回答

1个回答

热心网友

String time = new SimpleDateFormat("yyyyMMddHHmmss")
.format(Calendar.getInstance().getTime());// 得到系统时间
// 上传技术
SmartUpload up = new SmartUpload();
// 进行初始化
up.initialize(this.getServletConfig(), request, response);
// 开始上传
try {

up.upload("utf-8");//设置编码方式。
int id = Integer.parseInt(up.getRequest().getParameter("id"));// 商品编号
SmartFiles sf = up.getFiles();// 得到上传的所有图片
SmartFile file = sf.getFile(0);// 根据索引得到上传图片 多个图片可以用循环:
String type = file.getFileExt();// 得到图片后缀名
String folder = "tp/";// 指定文件夹
String path = folder + time + "." + type;// 路径
System.out.println(path + "路径");
file.saveAs(request.getRealPath("/") + path);// 保存图片
} catch (Exception e) {
e.printStackTrace();
}
//你搞个邮箱我把SmartUploadjar包 发给你吧。 //设置from提交
/*<form action="SellerServet" method="post"
enctype="multipart/form-data">*/ // 加上 enctype="multipart/form-data

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com