java不同分辨率图片上传

转载 2015年11月20日 16:09:35
public static boolean compressPic(byte[] arr, String path, String name, File dst, int width, int height,
boolean isScale) {
// 图片流
InputStream bufin = new ByteArrayInputStream(arr);
FileOutputStream out = null;
boolean b = false;
try {
// 图片流
BufferedImage img = ImageIO.read(bufin);
int srcWidth = img.getWidth(null);
int srcHeight = img.getHeight(null);
if ((srcHeight > height) || (srcWidth > width)) {
// 判断是否是等比缩放
if (isScale) {
double rate = Math.max((double) srcWidth / width, (double) srcHeight / height);
width = (int) (srcWidth / rate);
height = (int) (srcHeight / rate);

}

BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Image image = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(image, 0, 0, null);
out = new FileOutputStream(dst);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.flush();
} else {
saveImage(dst, arr, path, name);
}
b = true;
} catch (IOException e) {
e.printStackTrace();
}
return b;

}

ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileInputStream fis = (FileInputStream) inputStream;
int len = 0;
byte[] b = new byte[BUFFER_SIZE];
while ((len = fis.read(b)) != -1) {
bos.write(b, 0, len);
}
byte[] byteArr = bos.toByteArray();
saveImage(realImageFile, byteArr, path, realRelative); // 原图保存
compressPic(byteArr, path, relative, imageFile, 400, 300);// 大图,放大保存
compressPic(byteArr, path, smallRelative, smllImageFile, 180, 120);// 小图,缩小保存


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

struts2实现图片上传

在struts2中实现(以图片上传为例)
1.FileUpload.jsp代码清单如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>The FileUplaodDemo In Struts2</title>
</head>

<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data" namespace="/">
<s:file name="myFile" label="MyFile" ></s:file>
<s:textfield name="caption" label="Caption"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>

2.ShowUpload.jsp的功能清单如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>ShowUpload</title>
</head>

<body>
<div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
<img src ="UploadImages/<s:property value ="imageFileName"/> "/>
<br />
<s:property value ="caption"/>
</div >
</body>
</html>

3.FileUploadAction.java的代码清单如下 :

package com.chris;

import java.io.*;
import java.util.Date;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ;

//注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
//所以同时要提供myFileContentType,myFileFileName的set方法

private File myFile; //上传文件
private String contentType;//上传文件类型
private String fileName; //上传文件名
private String imageFileName;
private String caption;//文件说明,与页面属性绑定

public void setMyFileContentType(String contentType) {
System.out.println("文件类型 : " + contentType);
this .contentType = contentType;
}

public void setMyFileFileName(String fileName) {
System.out.println("文件名称 : " + fileName);
this .fileName = fileName;
}

public void setMyFile(File myFile) {
this .myFile = myFile;
}

public String getImageFileName() {
return imageFileName;
}

public String getCaption() {
return caption;
}

public void setCaption(String caption) {
this .caption = caption;
}

private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}

@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("UploadImages" ) + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}

注:此时仅为方便实现Action所以继承ActionSupport,并Overrider execute()方法
在struts2中任何一个POJO都可以作为Action

4.struts.xml清单如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="fileUpload" class="com.chris.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<result>/ShowUpload.jsp</result>
</action>
</package>
</struts>

5.web.xml清单如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter >
<filter-name > struts-cleanup </filter-name >
 <filter-class >
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class >
 </filter >
 <filter-mapping >
<filter-name > struts-cleanup </filter-name >
 <url-pattern > /* </url-pattern >
 </filter-mapping >

 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
<filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
 <welcome-file>Index.jsp</welcome-file>
 </welcome-file-list>

</web-app>

相关文章推荐

java批量图片上传

  • 2016-08-23 08:44
  • 8.84MB
  • 下载

Java实现图片上传

  • 2017-04-21 12:32
  • 3.45MB
  • 下载

Java抓取网络图片上传七牛云全纪录

前言项目想把图片上传到七牛云存储上,笔者把七牛最简单的用法示范一下,方便初学者。如果您还没有注册七牛,可以通过我的邀请链接注册 https://portal.qiniu.com/signup?code...

java 多张图片上传

简单多图片上传 jquery+java 代码

简单多图片上传 jquery+java 代码

java批量图片上传

  • 2017-01-13 15:57
  • 1.21MB
  • 下载

java实现图片上传预览

  • 2012-05-03 18:52
  • 392KB
  • 下载

java的图片上传与前端展示实例(Servlet+Jsp)

图片上传与前端展示实例内容前瞻 1、关于路径 2、前端图片上传与展示 3、后台获取并保存 环境 1、前端jsp 2、服务器tomcat 3、服务端servlet 4、使用tomcat发布项目的相对路径...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)