package Controller;
import Mapper.SQL_GoodsInformation;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import pojo.GoodsInformation;
import pojo.UserInformation;
import util.FileUploadUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/enterprise_modifyService")
public class enterprise_modifyService extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
System.out.println("进来了修改的页面");
GoodsInformation goodsInformation=new GoodsInformation();
try{
DiskFileItemFactory factory=new DiskFileItemFactory();
String filePath=this.getServletConfig().getServletContext().getRealPath("/static/img");
System.out.println("地址为"+filePath);
File f =new File(filePath);
if(!f.exists()){
System.out.println("不存在这个文件夹,开始创建");
f.mkdirs();
}
factory.setRepository(f);
//设置缓存大小
factory.setSizeThreshold(1024*1024*10);
ServletFileUpload fileUpload=new ServletFileUpload(factory);
fileUpload.setHeaderEncoding("UTF-8");
List<FileItem> fileitems=fileUpload.parseRequest(request);
PrintWriter writer=response.getWriter();
UserInformation userInformation=(UserInformation) request.getSession().getAttribute("user");
for (FileItem fileitem:fileitems) {
goodsInformation.setSeller_name(userInformation.getUser_name());
//判断施工欧是上传的组件
if(fileitem.isFormField())
{
//不是上传的组件
String name=fileitem.getFieldName();
if(name.equals("goods_name")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setGoods_name(value);
}
if(name.equals("goods_type")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setGoods_type(Integer.parseInt(value));
}
if(name.equals("priviewInformation")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setPriviewInformation(value);
}
if(name.equals("sell_price")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setSell_price(Double.parseDouble(value));
}
if(name.equals("surplus")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setSurplus(Integer.parseInt(value));
}
if(name.equals("original_price")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setOriginal_price(Integer.parseInt(value));
}
if(name.equals("goods_id")){
//如果文件不为空,保存在value中
String value=fileitem.getString("UTF-8");
goodsInformation.setGoods_id(Integer.parseInt(value));
}
}else {
String filename = fileitem.getName();//获取上传文件的名字
filename= FileUploadUtils.subFileName(filename);//获取文件的真实名字
String RandomName=FileUploadUtils
.generateRandonFileName(filename);
//System.out.println("上传文件的名字为" + filename);
// 随机生成目录
String randomDir = FileUploadUtils
.generateRandomDir( RandomName);
//System.out.println(randomDir);
String imgurl_parent = "/static/img"+randomDir;
//System.out.println("imgurl_parent " +imgurl_parent);
File parentDir = new File(this.getServletContext()
.getRealPath(imgurl_parent));
// 验证目录是否存在,如果不存在,创建出来
if (!parentDir.exists()) {
parentDir.mkdirs();
}
String imgurl = imgurl_parent + "/" + RandomName;
// System.out.println("图片的地址为 "+imgurl);
//这个是图片的真实地址
IOUtils.copy(fileitem.getInputStream(), new FileOutputStream(
new File(parentDir, RandomName)));
fileitem.delete();
goodsInformation.setBanner_img(randomDir+"/"+RandomName);
}
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("修改商品的信息"+goodsInformation);
int reult= SQL_GoodsInformation.UpdateGoods(goodsInformation);
if(reult>0){
System.out.println("修改商品成功");
}
}
}
下面是工具类,生成 唯一字符串
package util;
import java.util.UUID;
public class FileUploadUtils {
/**
* 截取真实文件名
*
* @param fileName
* @return
*/
public static String subFileName(String fileName) {
// 查找最后一个 \出现位置
int index = fileName.lastIndexOf("\\");
if (index == -1) {
return fileName;
}
return fileName.substring(index + 1);
}
// 获得随机UUID文件名
public static String generateRandonFileName(String fileName) {
// 获得扩展名
int index = fileName.lastIndexOf(".");
if (index != -1) {
String ext = fileName.substring(index);
return UUID.randomUUID().toString() + ext;
}
return UUID.randomUUID().toString();
}
// 获得hashcode生成二级目录
public static String generateRandomDir(String uuidFileName) {
int hashCode = uuidFileName.hashCode();
// 一级目录
int d1 = hashCode & 0xf;
// 二级目录
int d2 = (hashCode >> 4) & 0xf;
return "/" + d1 + "/" + d2;
}
}
下面的项目的结构,请看图片
还要导入两个包
commons-fileupload-1.4.jar
commons-io-2.6.jar
这两个包在我的 lib 里面
下面是我的项目
项目用的是idea编辑器
链接:https://pan.baidu.com/s/1RKK6_T68u1q5hf1JLDW87Q
提取码:rxq0
复制这段内容后打开百度网盘手机App,操作更方便哦