法1.借助StringBuilder 下面是实际开发时使用例子: 在extjs提供的HtmlEditor的基础上进行优化,原有的HtmlEditor不支持图片上传,修改后的HtmlEditor代码如下: HTMLEditor = Ext.extend(Ext.form.HtmlEditor, { ??????? var win = new Ext.Window({ ??????? }); 有了以上这些,然后调用StarHtmleditor即可在原有的htmleditor组件中显示一个图片按钮控件,如果仅做这些还不够,还要看图片的存储位置,在以下jsp代码中调用了好多特殊方法,值得借鉴: <%@ page import="static com.wlcsp.project.core.AppService.getappService" %> %> replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:?
String str="abcd123";
StringBuilder sb=new StringBuilder(str);
str=sb.reverse().toString;
法2.利用循环
String str="abcd123";
char[] chs=str.toCharArray();
int size=str.length();
char temp;
for(int i=0;i<size/2;i++){
temp=chs[i];
chs[i]=chs[size-1-i];
chs[size-1-i]=temp;
}
str=new String(chs);//翻转后的字符串
??? addImage : function() {
??????? var editor = this;
??????? var imgform = new Ext.FormPanel({
??????????? region : 'center',
??????????? labelWidth : 55,
??????????? frame : true,
??????????? bodyStyle : 'padding:5px 5px 0',
??????????? autoScroll : true,
??????????? border : false,
??????????? fileUpload : true,
??????????? items : [
??????????????? {
??????????????????? xtype : 'textfield',
??????????????????? fieldLabel : '选择文件',
??????????????????? name : 'userfile',
??????????????????? inputType : 'file',
??????????????????? allowBlank : false,
??????????????????? blankText : '文件不能为空',
??????????????????? height : 25,
??????????????????? anchor : '90%'
??????????????? }
??????????? ],
??????????? buttons : [
??????????????? {
??????????????????? text : '上传',
??????????????????? type : 'submit',
??????????????????? handler : function() {
??????????????????????? if (!imgform.form.isValid()) {
??????????????????????????? return;
??????????????????????? }
??????????????????????? imgform.form.submit({
??????????????????????????? waitMsg : '正在上传',
??????????????????????????? url : 'EditImg.jsp',
??????????????????????????? success : function(form, action) {
??????????????????????????????? var element = document.createElement("img");
????????????????????????????????var responseArray = Ext.util.JSON.decode(action.response.responseText);
????????????????????????????????if (responseArray.success) {
??????????????????????????????????? var qty = responseArray.fileURL;???//注意此处是如何从后台jsp页面获取参数的(同时jsp页面是如何向js页面传参数的,后面的jsp页面代码有标注)
??????????????????????????????????? element.src = qty;
??????????????????????????????????? editor.insertAtCursor(element.outerHTML);
??????????????????????????????????? win.hide();
??????????????????????????????? }
??????????????????????????? }
??????????????????????????????????????????????????? });
??????????????????? }
??????????????? },
??????????????? {
??????????????????? text : '关闭',
??????????????????? type : 'submit',
??????????????????? handler : function() {
??????????????????????? win.close(this);
??????????????????? }
??????????????? }
??????????? ]
??????? })
??????????? title : "上传图片",
??????????? width : 300,
??????????? height : 200,
??????????? modal : true,
??????????? border : false,
??????????? iconCls : "img.gif",
??????????? layout : "fit",
??????????? items : imgform
??????? win.show();
??? },
??? createToolbar : function(editor) {
??????? HTMLEditor.superclass.createToolbar.call(this, editor);
??????? this.tb.insertButton(16, {
??????????? cls : "x-btn-icon",
??????????? icon : "img.gif",
??????????? handler : this.addImage,
??????????? scope : this
??????? });
??? }
});
Ext.reg('StarHtmleditor', HTMLEditor);
<%@ page import="com.wlcsp.project.core.AppRequest" %>
<%@ page import="com.wlcsp.project.core.AppTemplate" %>
<%@ page import="com.wlcsp.project.bean.frameapp.kms.KmsDocument" %>
<%@ page import="com.wlcsp.project.bean.frameapp.kms.KmsDocumentAttachment" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="com.wlcsp.project.biz.SMBManager" %>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
??? final AppTemplate template = new AppTemplate(request, response, config, pageContext);
??? final AppRequest req = new AppRequest(request);
%>
<%
??? mySmartUpload.init(config);
??? mySmartUpload.service(request, response);
??? mySmartUpload.upload();
??? String filepath = "";
??? String filename = "";
??? String fileurl = "";
??? String name = "";
??? String systemname = "";
??? for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
??????? com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
??????? if (!myFile.isMissing()) {
??????????? filename = myFile.getFileName();
??????????? name = myFile.getFileName();
??????????? int filesize = myFile.getSize();
??????????? Timestamp timestamp = new Timestamp(System.currentTimeMillis());
??????????? java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
??????????? String creattime = sdf.format(timestamp);
??????????? systemname = creattime + name;
??????????? myFile.saveAs("/docfile/kms/" + systemname);
??????????? String s =?template.getRealPath("/");
????????????StringBuilder sb = new StringBuilder(s);
??????????? s = sb.reverse().toString();
??????????? s = s.substring(2, s.length());
??????????? sb = new StringBuilder(s);
??????????? s = sb.reverse().toString();
??????????? String path =?SMBManager.getConfig().getKmsfile();
??????????? String templateFileName = s + path;
??????????? templateFileName = templateFileName + systemname;
??????????? templateFileName =?templateFileName.replace("\\", "/");
??????????? fileurl = "E:/svn/new/Project/web/docfile/kms/" + systemname;
????????????out.print("{success:true,fileURL:'" + templateFileName + "'}");
??????? }
??? }
1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);?
2)replaceAll的参数是regex,即基于规则表达式的替换,比如,可以通过replaceAll("\\d", "*")把一个字符串所有的数字字符都换成星号;?
??? 相同点是都是全部替换,即把源字符串中的某一字符或字符串全部换成指定的字符或字符串,如果只想替换第一次出现的,可以使用 replaceFirst(),这个方法也是基于规则表达式的替换,但与replaceAll()不同的是,只替换第一次出现的字符串;?
??? 另外,如果replaceAll()和replaceFirst()所用的参数据不是基于规则表达式的,则与replace()替换字符串的效果是一样的,即这两者也支持字符串的操作;?
??? 还有一点注意:执行了替换操作后,源字符串的内容是没有发生改变的.?
举例如下:?
????????
??????? String src = new String("ab43a2c43d");?
??????? System.out.println(src.replace("3","f"));=>ab4f2c4fd.?
??????? System.out.println(src.replace('3','f'));=>ab4f2c4fd.?
??????? System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.?
??????? System.out.println(src.replaceAll("a","f"));=>fb43fc23d.?
??????? System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d?
??????? System.out.println(src.replaceFirst("4","h"));=>abh32c43d.?
如何将字符串中的"\"替换成"\\":?
??????? String msgIn;?
??????? String msgOut;?
??????? msgOut=msgIn.replaceAll("\\\\","\\\\\\\\");?
原因:?
???????? '\'在java中是一个转义字符,所以需要用两个代表一个。例如System.out.println( "\\" ) ;只打印出一个"\"。但是'\'也是正则表达式中的转义字符(replaceAll 的参数就是正则表达式),需要用两个代表一个。所以:\\\\被java转换成\\,\\又被正则表达式转换成\。?
同样?
??????? CODE: \\\\\\\\?
??????? Java: \\\\?
??????? Regex: \\?
将字符串中的'/'替换成'\'的几种方式:?
msgOut= msgIn.replaceAll("/", "\\\\");?
???????? msgOut= msgIn.replace("/", "\\");?
???????? msgOut= msgIn.replace('/', '\\');
详细解决方案
java中怎么反向显示字符串(包含实际开发中的例子)(转载)
热度:160 发布时间:2012-10-23 12:12:22.0
相关解决方案
- java 乱码 汉字是 ? 如何转换成汉字
- java web 登录次数限制,该如何解决
- java 工商银行网银支付 B2B的 都亟需什么,需要注意什么
- Java Applet程序从JDK6升级到7时遇到的有关问题
- java web 视频相干
- 请教一个关于链接后缀的有关问题(java web 应用)
- java 系统单点登录解决方案
- java.lang.NumberFormatException: For input string: "id"该如何处理
- java.lang.NoSuchMethodException: setId([Ljava.lang.String;)解决方法
- java 获取客户端IP解决办法
- JAVA 后台怎么获得前台页面FCKedit编辑器中的内容以及内容的样式
- java 正则化匹配有关问题
- java web 受阻
- java.lang.IllegalStateException: No output folder,该怎么解决
- java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccess,该如何解决
- java 乱码有关问题 急求大神
- java EE错误如何解决阿,都是过了,没有能解决的!
- java 工程打包有关问题 多谢各位大神!多谢
- JAVA WEB导航条,该怎么处理
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password,该如何处理
- java 中Node 有关问题
- 关于 java 引述传递和值传递,你的知否
- 吐了,java Timer 终止不了。
- Java Web 学习中有关问题,请高手指教
- java web中的url地址小疑点。
- java web开发解决办法
- java.lang.NullPointerException解决方法
- 关于 java cast 有关问题,你明白不
- java ftp 下传和上载乱码有关问题
- java web项目上的一些文件