<! - jsp添加图文件页面 - >
<%@ page language =“java”import =“java.util。*”pageEncoding =“UTF-8”%>
String path = request.getContextPath();
String basePath = request.getScheme()+“://”
+ request.getServerName()+“:”+ request.getServerPort()
+ path +“/”;
%>
<!DOCTYPE HTML PUBLIC“ - // W3C // DTD HTML 4.01 Transitional // EN”>
<html>
<head>
<base href =“<%= basePath%>”>
<title> My JSP'index。 jsp'首页'
<meta http-equiv =“pragma”content =“no-cache”>
<meta http-equiv =“cache-control”content =“no-cache”>
<meta http-equiv = “
</ head>
<body>
<div class =“container”style =“width:60%”>
<form class =“form-horizontal”action =“uploadProduct.action”
method =“post”enctype =“multipart / form -data“>
<div class =”form-group“>
<label class =”control-label col-md-3“>产品图片</
div > <div class =”col-md-7“>
<input type =“file”name =“pic”class =“form-control”/>
</ div>
</ div>
<div class =“form-group”>
<label class =“control-label col-md-3 “>图片描述:</
div > <div class =”col-md-7“>
<textarea name =”descr“style =“height:80px”class =“form-control”> </ textarea>
</ div>
</ div>
<input type =“text”name =“fileName”/>
<div class =“form-group” >
<div class =“col-md-offset-4”>
数据){ $(“[name = fileName]”)。val(data); }); </ script> </ body> </ html>
//的servlet处理文件
package upload;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import net.coobird.thumbnailator.Thumbnails;
@WebServlet(“/ uploadByAjax.action”)
@MultipartConfig
public class UploadByAjaxAction extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Part part=req.getPart("pic");//获取二进制信息,Part封装上传的二进制信息
String suffix=getSuffix(part);//获取上传文件的后缀名
//采用UUID得到一个新文件名(具备唯一性)
String newName=UUID.randomUUID().toString().replace("-", "")+suffix;
execute(part, "b"+newName,200,"bigPic");
execute(part, "i"+newName, 50,"icon");
PrintWriter out=resp.getWriter();
out.print(newName);
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Part part=req.getPart("pic");//获取二进制信息,Part封装上传的二进制信息
String suffix=getSuffix(part);//获取上传文件的后缀名
//采用UUID得到一个新文件名(具备唯一性)
String newName=UUID.randomUUID().toString().replace("-", "")+suffix;
execute(part, "b"+newName,200,"bigPic");
execute(part, "i"+newName, 50,"icon");
PrintWriter out=resp.getWriter();
out.print(newName);
out.flush();
out.close();
}
private void execute(Part part, String newName,int width,String position) throws IOException {
System.out.println(position);
//动态获取上传目标
String realPath=this.getServletContext().getRealPath("/"+position+"/");
System.out.println(realPath);
File target=new File(realPath);
//上传目录不存在,则创建目录
if(target.exists()==false){
target.mkdirs();
}
//part.write(realPath+newName); //把上传二进制信息写入新文件中
Thumbnails.of(part.getInputStream()).width(width).toFile(realPath+newName);
}
private String getSuffix(Part part){
String disp=part.getHeader("content-disposition");
int beginIndex=disp.lastIndexOf(".");
int endIndex=disp.lastIndexOf("\"");
String suffix=disp.substring(beginIndex, endIndex);
return suffix;
}
System.out.println(position);
//动态获取上传目标
String realPath=this.getServletContext().getRealPath("/"+position+"/");
System.out.println(realPath);
File target=new File(realPath);
//上传目录不存在,则创建目录
if(target.exists()==false){
target.mkdirs();
}
//part.write(realPath+newName); //把上传二进制信息写入新文件中
Thumbnails.of(part.getInputStream()).width(width).toFile(realPath+newName);
}
private String getSuffix(Part part){
String disp=part.getHeader("content-disposition");
int beginIndex=disp.lastIndexOf(".");
int endIndex=disp.lastIndexOf("\"");
String suffix=disp.substring(beginIndex, endIndex);
return suffix;
}
}