|
@@ -6,6 +6,7 @@ import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -429,7 +430,7 @@ public class AdController extends SimpleController {
|
|
|
*文件上传
|
|
|
* */
|
|
|
@RequestMapping("/upload")
|
|
|
- public @ResponseBody String upload(HttpServletRequest request,HttpServletResponse response,Model model,Long id) throws IOException{
|
|
|
+ public @ResponseBody String upload(HttpServletRequest request,HttpServletResponse response,Model model,Long bannerId) throws IOException{
|
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
String uploadName = null;
|
|
@@ -437,6 +438,7 @@ public class AdController extends SimpleController {
|
|
|
String pathSmall = null;
|
|
|
String fileType = null;
|
|
|
String result = "null";
|
|
|
+ BannerTemplate bannerTemplate = bannerService.findById(bannerId).getBannerTemplate();
|
|
|
// 创建一个通用的多部分解析器
|
|
|
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
|
|
|
// 判断 request 是否有文件上传,即多部分请求
|
|
@@ -460,6 +462,8 @@ public class AdController extends SimpleController {
|
|
|
result = "~不支持此文件类型";
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
//判断文件名书否存在
|
|
|
if(uploadName.trim() !=""){
|
|
|
|
|
@@ -469,27 +473,21 @@ public class AdController extends SimpleController {
|
|
|
//获取上传路径
|
|
|
path = getLocalFilePath(fileName);
|
|
|
|
|
|
- try{
|
|
|
+ try{
|
|
|
//新建上传路径下文件
|
|
|
File localFile = new File(path);
|
|
|
if (!localFile.getParentFile().exists()){
|
|
|
localFile.getParentFile().mkdirs();
|
|
|
}
|
|
|
- //if(checkPic(path)){
|
|
|
+
|
|
|
//复制文件到上传文件
|
|
|
file.transferTo(localFile);
|
|
|
- //取上传文件父级目录
|
|
|
- String absolutePath = localFile.getParentFile().getAbsolutePath();
|
|
|
- //生成缩略图路径名
|
|
|
- pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
|
|
|
- //新建缩略图文件
|
|
|
- smallPicture(localFile,pathSmall, uploadName);
|
|
|
-
|
|
|
- result="文件上传成功";
|
|
|
-// }else{
|
|
|
-// result="图片太大了哦!";
|
|
|
-// }
|
|
|
- }catch(IOException e){
|
|
|
+ if(checkPic(localFile,bannerTemplate)){
|
|
|
+ result = "~图片尺寸错啦!!";
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch(IOException e){
|
|
|
e.printStackTrace();
|
|
|
result="~文件上传失败 ";
|
|
|
return result;
|
|
@@ -514,19 +512,26 @@ public class AdController extends SimpleController {
|
|
|
@RequestMapping("/save")
|
|
|
public String save(HttpServletRequest request, Model model, @ModelAttribute("form") Banner banner,
|
|
|
@RequestParam Map<String, Object> paramMap) throws IOException {
|
|
|
-
|
|
|
-
|
|
|
+ //图片宽高
|
|
|
+ int width;
|
|
|
+ int height;
|
|
|
+ //广告位宽高
|
|
|
+ int _width;
|
|
|
+ int _height;
|
|
|
//查找创意
|
|
|
Banner formerBanner = bannerService.findById(banner.getId());
|
|
|
//取模板素材
|
|
|
BannerTemplate bannerTemplate = formerBanner.getBannerTemplate();
|
|
|
|
|
|
+
|
|
|
if(paramMap.containsKey("uploadPath")&¶mMap.get("uploadPath")!=""
|
|
|
&¶mMap.get("uploadPath")!=null){
|
|
|
- String uploadName = bannerTemplate.getName();
|
|
|
+ String uploadName = banner.getBannerTemplate().getName();
|
|
|
String fileType = uploadName.substring(uploadName.lastIndexOf(".")+1);
|
|
|
String filePath = (String)paramMap.get("uploadPath");
|
|
|
- /**生成文件缩略图*/
|
|
|
+
|
|
|
+
|
|
|
+ //取上传文件位置
|
|
|
File uploadFile = new File(filePath);
|
|
|
if(!uploadFile.getParentFile().exists()){
|
|
|
uploadFile.getParentFile().mkdirs();
|
|
@@ -535,14 +540,28 @@ public class AdController extends SimpleController {
|
|
|
String absolutePath = uploadFile.getParentFile().getAbsolutePath();
|
|
|
//生成缩略图路径名
|
|
|
String pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
|
|
|
- smallPicture(uploadFile,pathSmall, uploadName);
|
|
|
-
|
|
|
- //匹配广告位宽高
|
|
|
-
|
|
|
- bannerTemplate = suitSize(filePath, bannerTemplate);
|
|
|
- //设置文件上传路径
|
|
|
- bannerTemplate.setPath(replacePath(filePath));
|
|
|
+ //生成缩略图
|
|
|
+ compressPicture(uploadFile,pathSmall, 150,60);
|
|
|
|
|
|
+ //取上传图片的宽高
|
|
|
+ BufferedImage image = ImageIO.read(new FileInputStream(filePath));
|
|
|
+ width = image.getWidth();
|
|
|
+ height = image.getHeight();
|
|
|
+ _width =Integer.parseInt(bannerTemplate.getWidth().toString()) ;
|
|
|
+ _height = Integer.parseInt(bannerTemplate.getHeight().toString());
|
|
|
+
|
|
|
+ if(width>_width||height>_height){
|
|
|
+ //设置压缩文件上传路径
|
|
|
+ String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(uploadName);
|
|
|
+ //生成压缩图
|
|
|
+ compressPicture(uploadFile,pathSmall, _width,_height);
|
|
|
+ //设置文件上传路径
|
|
|
+ bannerTemplate.setPath(replacePath(compressPath));
|
|
|
+ }else{
|
|
|
+
|
|
|
+ //设置文件上传路径
|
|
|
+ bannerTemplate.setPath(replacePath(filePath));
|
|
|
+ }
|
|
|
//设置缩略图上传路径
|
|
|
bannerTemplate.setName(banner.getBannerTemplate().getName());
|
|
|
bannerTemplate.setPathSmall(replacePath(pathSmall));
|
|
@@ -553,6 +572,7 @@ public class AdController extends SimpleController {
|
|
|
banner.setBannerTemplate(bannerTemplate);
|
|
|
banner.setChecked(0);
|
|
|
bannerService.updata(banner);
|
|
|
+
|
|
|
}else{
|
|
|
bannerService.edit(banner);
|
|
|
}
|
|
@@ -877,65 +897,12 @@ public class AdController extends SimpleController {
|
|
|
|
|
|
|
|
|
|
|
|
- //取素材的大小
|
|
|
- public BannerTemplate setWidthAndHeight(BannerTemplate bannerTemplate,String urls,String fileType){
|
|
|
-
|
|
|
- if("jpeg".equalsIgnoreCase(fileType)||"jpg".equalsIgnoreCase(fileType) ||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType)){
|
|
|
- //把文件读取到本地
|
|
|
- File file = new File(urls);
|
|
|
- FileInputStream istream = null;
|
|
|
- try {
|
|
|
- istream = new FileInputStream(file);
|
|
|
- } catch (FileNotFoundException e1) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
- BufferedImage image=null;
|
|
|
- try {
|
|
|
- image = javax.imageio.ImageIO.read(istream);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- if(image!=null){
|
|
|
- bannerTemplate.setWidth((long)image.getWidth());
|
|
|
- bannerTemplate.setHeight((long)image.getHeight());
|
|
|
- }else{
|
|
|
- System.out.println("aaaa");
|
|
|
- }
|
|
|
- try {
|
|
|
- istream.close();
|
|
|
- } catch (IOException e) {
|
|
|
-
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }else if("swf".equalsIgnoreCase(fileType)){ //swf文件类型的设置
|
|
|
- File file=new File(urls);
|
|
|
- SWFHeader swfh = new SWFHeader();
|
|
|
- try {
|
|
|
- swfh = SWFHeader.load(file);
|
|
|
- bannerTemplate.setWidth((long)swfh.getWidth());
|
|
|
- bannerTemplate.setHeight((long)swfh.getHeight());
|
|
|
- } catch (IOException e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
-
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
-
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return bannerTemplate;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
|
|
|
//处理上传文件的绝对路径
|
|
|
public String getLocalFilePath(String fileName){
|
|
|
- Account loginUser = getLoginUser();
|
|
|
- // 计算相对路径名,即不包括默认路径名
|
|
|
- String path = new StringBuilder()
|
|
|
+ Account loginUser = getLoginUser();
|
|
|
+ // 计算相对路径名,即不包括默认路径名
|
|
|
+ String path = new StringBuilder()
|
|
|
.append(File.separator)
|
|
|
.append(loginUser == null ? "anon" : loginUser.getId())
|
|
|
.append(File.separator)
|
|
@@ -947,106 +914,103 @@ public class AdController extends SimpleController {
|
|
|
.toString();
|
|
|
|
|
|
String localFilePath =config.getResourceUploadDir().concat(File.separator).concat("banner").concat(path);
|
|
|
- //String localFilePath = "/upload/banner".concat(path);
|
|
|
|
|
|
- String accessUrl = localFilePath;
|
|
|
- ServletContext servletContext = getServletContext();
|
|
|
+ String accessUrl = localFilePath;
|
|
|
+ ServletContext servletContext = getServletContext();
|
|
|
if (servletContext == null) {
|
|
|
LOG.debug("单体测试情况,servletContext不存在。");
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
|
|
|
accessUrl = servletContext.getRealPath("/");
|
|
|
File upFile = new File(accessUrl);
|
|
|
accessUrl = upFile.getParent();
|
|
|
LOG.info("\naaa----"+localFilePath+" -----"+accessUrl);
|
|
|
- // 获取绝对路径
|
|
|
+ //获取绝对路径
|
|
|
localFilePath = accessUrl.concat(File.separator).concat(localFilePath);
|
|
|
}
|
|
|
- return localFilePath;
|
|
|
- }
|
|
|
+ return localFilePath;
|
|
|
+ }
|
|
|
|
|
|
//处理路径为web路径
|
|
|
public String replacePath(String path){
|
|
|
|
|
|
-
|
|
|
-// String url = config.getResourceUploadDomain()
|
|
|
-// .concat(":")
|
|
|
-// .concat(config.getResourceUploadPort())
|
|
|
-// .concat("/");
|
|
|
path = path.substring(path.indexOf("upload")-1);
|
|
|
-// path = path.replace(top, url);
|
|
|
+
|
|
|
return path;
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ //处理文件类型
|
|
|
private int judgeType(String fileType){
|
|
|
- int type = 2;
|
|
|
- if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
|
|
|
- type = 2;
|
|
|
- }else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
|
|
|
- type = 3;
|
|
|
- }else{
|
|
|
- type = 1;
|
|
|
- }
|
|
|
- return type;
|
|
|
- }
|
|
|
+ int type = 2;
|
|
|
+ if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
|
|
|
+ type = 2;
|
|
|
+ }else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
|
|
|
+ type = 3;
|
|
|
+ }else{
|
|
|
+ type = 1;
|
|
|
+ }
|
|
|
+ return type;
|
|
|
+ }
|
|
|
|
|
|
//生成缩略图
|
|
|
- public void smallPicture(File localFile,String pathSmall,String uploadName) throws IOException{
|
|
|
+ public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
|
|
|
//新建缩略图文件
|
|
|
- File smallFile = new File(pathSmall);
|
|
|
- if (!smallFile.getParentFile().exists()){
|
|
|
- smallFile.getParentFile().mkdirs();
|
|
|
+ File newFile = new File(pathNew);
|
|
|
+ if (!newFile.getParentFile().exists()){
|
|
|
+ newFile.getParentFile().mkdirs();
|
|
|
}
|
|
|
//构造image图片
|
|
|
Image src = javax.imageio.ImageIO.read(localFile);
|
|
|
- /**得到原图的宽高
|
|
|
- int width = src.getWidth(null);
|
|
|
- int height =src.getHeight(null);*/
|
|
|
//表示该图像具有打包成整数的像素的8 位RGB颜色分量
|
|
|
- BufferedImage tag = new BufferedImage(150, 60, BufferedImage.TYPE_INT_BGR);
|
|
|
+ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
|
|
|
//h绘制缩小后的图片
|
|
|
- tag.getGraphics().drawImage(src, 0, 0, 150, 60, null);
|
|
|
+ tag.getGraphics().drawImage(src, 0, 0, width, height, null);
|
|
|
//生成缩略图
|
|
|
- ImageIO.write(tag, "png", smallFile);
|
|
|
+ ImageIO.write(tag, "png", newFile);
|
|
|
}
|
|
|
|
|
|
- //匹配广告为宽高,用取平方差最小
|
|
|
- public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws IOException{
|
|
|
- File file = new File(path);
|
|
|
+
|
|
|
+
|
|
|
+ //编辑创意判断上传图片是否符合上传条件
|
|
|
+ public boolean checkPic(File localFile,BannerTemplate bannerTemplate) throws IOException{
|
|
|
//构造image图片
|
|
|
- Image src = javax.imageio.ImageIO.read(file);
|
|
|
- int width = src.getWidth(null);
|
|
|
- int height =src.getHeight(null);
|
|
|
- Size size = new Size();
|
|
|
- int i;
|
|
|
- int n =0;
|
|
|
- //查找所有广告位
|
|
|
- List<Size> sizeList = sizeService.findAllSize();
|
|
|
- //取平方差
|
|
|
- double suit[] = new double[sizeList.size()];
|
|
|
- long _width[] = new long[sizeList.size()];
|
|
|
- long _height[] = new long[sizeList.size()];
|
|
|
- for ( i=0 ;i<sizeList.size();i++){
|
|
|
- size = sizeList.get(i);
|
|
|
- _width[i] = size.getWidth();
|
|
|
- _height[i] = size.getHeight();
|
|
|
- suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
|
|
|
- }
|
|
|
-
|
|
|
- double min =suit[0];
|
|
|
- for (i=0;i<suit.length;i++){
|
|
|
- if (suit[i]<min){
|
|
|
- min = suit[i];
|
|
|
- n=i;
|
|
|
- }
|
|
|
+ Image src = javax.imageio.ImageIO.read(localFile);
|
|
|
+ long width = src.getWidth(null);
|
|
|
+ long height = src.getHeight(null);
|
|
|
+ long _width = bannerTemplate.getWidth();
|
|
|
+ long _height = bannerTemplate.getHeight();
|
|
|
+ if(width>0&&height>0){
|
|
|
+ if(width<=_width&&height<=_height){
|
|
|
+ return false;
|
|
|
+ }else{
|
|
|
+ if((width*_height)==(_width*height)){
|
|
|
+ return false;
|
|
|
}
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- bannerTemplate.setWidth(_width[n]);
|
|
|
- bannerTemplate.setHeight(_height[n]);
|
|
|
- return bannerTemplate;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+// //如果是大于广告位且等比例的需要等比例缩小
|
|
|
+// public void compressPic(File localFile,String compressPath,) throws IOException{
|
|
|
+// File compressFile = new File(compressPath);
|
|
|
+// if (!compressFile.getParentFile().exists()){
|
|
|
+// compressFile.getParentFile().mkdirs();
|
|
|
+// }
|
|
|
+// //构造image图片
|
|
|
+// Image src = javax.imageio.ImageIO.read(localFile);
|
|
|
+//
|
|
|
+// //表示该图像具有打包成整数的像素的8 位RGB颜色分量
|
|
|
+// BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
|
|
|
+// //h绘制缩小后的图片
|
|
|
+// tag.getGraphics().drawImage(src, 0, 0, width, height, null);
|
|
|
+// //生成压缩图
|
|
|
+// ImageIO.write(tag, "png", compressFile);
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|