关于客户端上传图片和文字,服务器对上传的数据的接收和图片压缩处理
来源:互联网 发布:平均利润率下降知乎 编辑:程序博客网 时间:2024/06/03 18:08
//获取客户端传递上来的数据和图片
//客户端post提交,将request对象传递进来
public synchronized static boolean uploadreportImage(HttpServletRequest request){
boolean flag=false;String item=""; //客户端提交的字段
//临时文件名
String temp="D:/apache-tomcat-6.0.20/webapps/temp";
//上传文件存放目录
System.out.println(request.getSession().getServletContext());
System.out.println(loadpath);
DiskFileUpload upload=new DiskFileUpload();
//设置允许用户上传文件大小
upload.setFileSizeMax(10*1024*1024);
//设置只允许在内存中存放的数据大小
upload.setSizeThreshold(10*1024*1024);
//设置文件大小超过getSizeThreshold()的大小,数据存放入硬盘
upload.setRepositoryPath(temp);
upload.setHeaderEncoding("utf-8");
List fileItems = null;
try {
fileItems = upload.parseRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
Iterator iter = fileItems.iterator(); //
//获取客户端传递的图片
while (iter.hasNext()){FileItem item=(FileItem) iter.next();
if(!item.isFormField()){
name=item.getName();
long size=item.getSize();
if((name!=null && !"".equals(name)) && size>0){
name=System.currentTimeMillis()+".png";
// imagearray.add(name);
File fNew= new File(loadpath, name);
imageName+=name+",";
try {
item.write(fNew);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//对图片进行按width和height进行压缩,传递true压缩,传递false不进行压缩
CompressPicDemo mypic = new CompressPicDemo();mypic.compressPic(UplodImage.loadpath, UplodImage.newloadpath, name, name, 200, 200, false);
name="";
}
}else{
String fieldvalue;
try {
fieldvalue = item.getString("utf-8");
System.out.println("获取普通的数据>>>>>"+item.getFieldName()+"\t"+fieldvalue+"\t"+URLDecoder.decode(URLDecoder.decode(fieldvalue, "utf-8"), "utf-8"));
if(item.getFieldName().equals("item")){
item=URLDecoder.decode(URLDecoder.decode(fieldvalue, "utf-8"), "utf-8");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(imageName.trim()!=null && !"".equals(imageName.trim())){
imageName=imageName.substring(0, imageName.length()-1);
}else{
imageName="NOIMAGE";
}
return flag;
}
//图片压缩类
public class CompressPicDemo {
private File file = null; // 文件对象
private String inputDir; // 输入图路径
private String outputDir; // 输出图路径
private String inputFileName; // 输入图文件名
private String outputFileName; // 输出图文件名
private int outputWidth = 100; // 默认输出图片宽
private int outputHeight = 100; // 默认输出图片高
private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
public CompressPicDemo() { // 初始化变量
inputDir = "";
outputDir = "";
inputFileName = "";
outputFileName = "";
outputWidth = 100;
outputHeight = 100;
}
public void setInputDir(String inputDir) {
this.inputDir = inputDir;
}
public void setOutputDir(String outputDir) {
this.outputDir = outputDir;
}
public void setInputFileName(String inputFileName) {
this.inputFileName = inputFileName;
}
public void setOutputFileName(String outputFileName) {
this.outputFileName = outputFileName;
}
public void setOutputWidth(int outputWidth) {
this.outputWidth = outputWidth;
}
public void setOutputHeight(int outputHeight) {
this.outputHeight = outputHeight;
}
public void setWidthAndHeight(int width, int height) {
this.outputWidth = width;
this.outputHeight = height;
}
/*
* 获得图片大小
* 传入参数 String path :图片路径
*/
public long getPicSize(String path) {
file = new File(path);
return file.length();
}
// 图片处理
public String compressPic() {
try {
//获得源文件
file = new File(inputDir + inputFileName);
if (!file.exists()) {
return "";
}
Image img = ImageIO.read(file); //这里是从本地读图片文件,如果是执行上传图片的话, Formfile formfile=获得表单提交的Formfile ,然后 ImageIO.read 方法里参数放 formfile.getInputStream()
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
} else {
int newWidth; int newHeight;
// 判断是否是等比缩放
if (this.proportion == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = outputWidth; // 输出的图片宽度
newHeight = outputHeight; // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {
// 输入图路径
this.inputDir = inputDir;
// 输出图路径
this.outputDir = outputDir;
// 输入图文件名
this.inputFileName = inputFileName;
// 输出图文件名
this.outputFileName = outputFileName;
return compressPic();
}
public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
// 输入图路径
this.inputDir = inputDir;
// 输出图路径
this.outputDir = outputDir;
// 输入图文件名
this.inputFileName = inputFileName;
// 输出图文件名
this.outputFileName = outputFileName;
// 设置图片长宽
setWidthAndHeight(width, height);
// 是否是等比缩放 标记
this.proportion = gp;
return compressPic();
}
}
- 关于客户端上传图片和文字,服务器对上传的数据的接收和图片压缩处理
- php对图片上传的处理以及图片加文字水印和图片水印的方法
- 安卓端同时上传文字和图片到服务器,服务器接收并处理
- 图片上传存取图片路径和图片数据 以及给上传的图片加文字和图片水印
- Java后台服务器接收IOS客户端上传的图片
- 上传图片和文字
- javaweb接收客户端上传的图片
- Android上传图片和文字到服务器
- Android中图片 的上传和接收以及后台图片的上传和接收
- c# 实现对图片上传的接收
- 关于Android中图片压缩(上传前的处理)
- 关于利用asi上传图片,服务器那边接收不到数据
- 安卓端同时上传图片和文字,服务器端接收(二)
- 关于thinkphp5的上传图片和ckeditor
- 上传图片和图片处理
- okhttp方法上传图片和接收图片
- 控制器接收图片和上传图片写法
- h5图片上传和压缩
- Nutch学习与安装
- SAP 角色常用表
- 记录一下
- Failed to get local hostname java.net.UnknownHostException
- 客户端用https webservice
- 关于客户端上传图片和文字,服务器对上传的数据的接收和图片压缩处理
- 配置MongoDB集群分片
- jvm分析 记录下
- NDK处理用户交互事件
- critical_section临界资源日志管理
- JQuery 磁力图片
- gcc 从语言编译全过程 预处理---->编译---->汇编----->链接
- Library (computing)
- CUDA学习笔记之CUDA存储器模型