java 图形处理库_java中处理图片的类库
拷贝一下代码,以后备用:import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import com.aa.bb.Constants;
import com.aa.bb.util.ImageUtil;
import com.aa.bb.util.StringUtil;
import com.aa.bb.util.upload.GetURLUtil;
import com.aa.bb.util.upload.HandleUploadeFile;
import com.aa.bb.util.upload.MD5;
public class UDArticleImagesAction extends HttpServlet {
/**
* Constructor of the object.
*/
public UDArticleImagesAction() {
super();
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String body = request.getParameter("body");
String path = getServletContext().getRealPath(GetURLUtil.getWebArticleAffixPath() +
GetURLUtil.getPath() + Constants.YEAR + GetURLUtil.getPath() +
Constants.MONTH);
String fileName;
// 文章内容非空
if(!StringUtil.isBlank(body)) {
// 取得文章中所有IMG标签
List imgList = ImageUtil.getImgSrc(body);
HttpClient client = new HttpClient();
// 遍历下载并重命名
for(String img : imgList) {
// 取得URL中的文件名
fileName = img.split("/")[img.split("/").length - 1];
// get方法取得图片
GetMethod get = new GetMethod(img);
client.executeMethod(get);
// 建立文件存储路径
HandleUploadeFile.checkDirectoryAndCreateDirectory(path);
//图片名称
String imgName = MD5.getImgName(fileName);
// 存储文件
// System.out.println(path + "\\" + MD5.getImgName(fileName));
// File storeFile = new File(path + "\\" + MD5.getImgName(fileName));
System.out.println(path + "\\" + imgName);
File storeFile = new File(path + "\\" + imgName);
FileOutputStream output = new FileOutputStream(storeFile);
//得到网络资源的字节数组,并写入文件
output.write(get.getResponseBody());
output.close();
// 初始化新图片本地URL
String imgUrl = "http://" + request.getServerName();
if(request.getServerPort() != 80) {
imgUrl += ":" + request.getServerPort();
}
imgUrl += "/cms" + GetURLUtil.getWebArticleAffixPath() +
"/" + Constants.YEAR + "/" + Constants.MONTH + "/" + imgName;
System.out.println(imgUrl);
// 替换到原文章内容
body = StringUtil.replaceAll(body, img, imgUrl);
}
System.out.println(body);
out.println(body);
}
out.flush();
out.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("123");
out.flush();
out.close();
}
}
java 图形处理库_java中处理图片的类库相关推荐
- java pdf库_Java中常用的操作PDF的类库
iText iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好的给合.使用 ...
- java序列化库_java 中序列化(Serializable)
18年5月份,在毕业快一年的时候,我终于把自己从研发助理偏向行政打杂,偶尔写一些前端代码的处境,回归到了初衷-安安静静写前后端代码的小小程序员~过程中的心酸也就不在这里吐啦,毕竟我是小太阳,永不熄灭的 ...
- java 基础库_Java基础类库
包 1.java.lang包:java类库中的核心部分,包含System系统类.数学运算的Math类.处理字符串的String类以及8个数据类型包装类(Integer.Double.Float.Cha ...
- i10在java中什么意思啊,java题库_JAVA题库的题 求答案啊!!!!!!!!!!!!!!!!_淘题吧...
⑴ JAVA题库的题 求答案啊!!!!!!!!!!!!!!!! 1.有点没太明白意思,是要求2个数相加的和吗? 导入Java.util.扫描仪: 公共类应用程序1 /** * @param参数 */ ...
- java jceks 密钥_Java中不同类型的密钥库(Keystore) – 概述
阅读: 877 密钥库是用于存储加密密钥和证书的存储工具 ,最常用于SSL通信,以证明服务器和客户端的身份.密钥库可以是文件或硬件设备.有三种类型的条目可以存储在密钥库中,取决于密钥库的类型,这三种类 ...
- java支持库(_Java运行环境支持库下载-Java Runtime Environment(JRE) v9.1.8官方64位版-ucbug下载站...
JRE为Java Runtime Environment的简称,运行JAVA程序所必须的环境的集合,包含JVM标准实现及Java核心类库.Java Runtime Environment(包括Java ...
- java gc 例子_Java 中, 为什么一个对象的实例方法在执行完成之前其对象可以被 GC 回收?...
收到好多邀请-泻药泻药.其中有好些感觉是想来吃瓜的. 所以请让我先跑个题,为吃瓜群众献上几个传送门: private static void newSingleThreadPool() { Execu ...
- java 字符串总结_Java中字符串(String)总结
先说说JDK API: JDK中包含大量的API类库,所谓API(Application Programming Interface,应用程序编程接口,这些功能以类的形式封装). JDK API包含的 ...
- java set 空值_Java中如何更优雅的处理空值
经常看到项目中存在到处空值判断的情况,这些判断,会让人觉得摸不着头绪,它的出现很有可能和当前的业务逻辑并没有关系.但它会让你很头疼.有时候,更可怕的是系统因为这些空值的情况,会抛出空指针异常,导致业务 ...
最新文章
- NumPy — 创建全零、全1、空、arange 数组,array 对象类型,astype 转换数据类型,数组和标量以及数组之间的运算,NumPy 数组共享内存
- 算法---------二叉树的后序遍历
- mybatis面试题讲解1
- SSL 的 java 实现
- 五、Web服务器——MVC开发模式 EL表达式 JSTL 学习笔记
- LeetCode 1822. 数组元素积的符号
- WordPress 响应式全站 AJAX 主题:Beginning
- 小汤学编程之JDBC(二)——dbutils框架和DataSource连接池
- Android-- Dialog对话框的使用方法
- 局域网的分层结构的优点及分层原理
- Linux下的find指令分析 , atime, ctime, mtime
- 网游Server端开发基础
- 没有工厂模式(工厂模式2)
- Ubuntu dpkg 常用命令教程
- 等待事件之日志等待事件解决的方法
- mtk刷机显示连接服务器失败,MTK刷机常见错误分析 报错代码解释3
- 计算机考证要考PS吗
- excel工作簿合并为一个表格
- 使用Jquery+bootstrap无限级菜单树
- mp3怎么转换成m4r(iPhone铃声制作)
热门文章
- 打jar包和执行jar包
- 自定义背景_新版快绘精选:自定义背景墙 | 吊顶 | 云渲染滤镜升级
- JAVA四则运算(读写文件)
- oracle批量update
- 在 Apex 代码中生成 csv 文件
- 一个取消事件的简单js例子(事件冒泡与取消默认行为)
- jQuery操作cookie
- C++ Editbox换行问题
- [Leedcode][JAVA][第128题][最长连续序列][Hash]
- [剑指offer]面试题第[48]题[Leetcode][JAVA][第3题][无重复字符的最长字串][滑动窗口][HashSet/Map]