jsp中截取字符串的问题

sushou2009 2009-07-11 04:18:51
我在jsp页面中有一个控件<input type="file" name="picture" size="34">我想截取地址中的文件名,如地址是:E:\C\company\WebRoot\images\botton-cz.jpg。我想截取最后一个“\” 后面的所有字符存到数据库中。但是当我定义
String name = "E:\C\company\WebRoot\images\botton-cz.jpg";的时候提示我错误。请教高手这里该怎么解决?
...全文
4188 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
时光不等仁 2012-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

int index = image.lastIndexOf('\');
为什么这里老是提示我\那里错误,换成“”也不对。
[/Quote]
因为这里需要的是转义字符
sunset0118 2010-04-16
  • 打赏
  • 举报
回复
和谐 都是为了学习
zhugewuhou 2010-04-16
  • 打赏
  • 举报
回复
太长了吧
yueweiac110 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sushou2009 的回复:]

引用 8 楼 king4323210 的回复:
SB

转义符号都不认识!

SB会的话就不问了!
[/Quote]

不会就问,9楼你什么都会啊,顶楼主。
vitahuang 2010-04-16
  • 打赏
  • 举报
回复
MyJsp.jsp:

<form action="test.jsp">
<input type = "file" id = "pfile" name = "pfile" size = "34"/>
<input type = "submit" id = "submit" name = "submit"/>
</form>

test.jsp:

<body>
<% String path = request.getParameter("pfile");
String name = path.substring(path.lastIndexOf("\\")+1);
out.println(name);
%>
</body>
ysfxhxyyy 2010-04-16
  • 打赏
  • 举报
回复
String name = "E:\C\company\WebRoot\images\botton-cz.jpg";这样写报错很正常因为'\'代表的意思是转义例如\r代表换行。 如果你想截取字符串则
String image = request.getParameter("picture");
String str=image.replaceAll("\\",",");
int index = str.lastIndexOf(",");
String picName = image.substring(index + 1);


浮屠塔 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 macau2008 的回复:]
引用 9 楼 sushou2009 的回复:
引用 8 楼 king4323210 的回复:
SB

转义符号都不认识!


SB会的话就不问了!
![/Quote]
真是不知道你们有多厉害似的
lybjust 2009-07-13
  • 打赏
  • 举报
回复
String str="E:\\C\\company\\WebRoo\\images\\botton-cz.jpg";
int index=str.lastIndexOf("\\");
System.out.println(str.substring(index+1));
码YouMe 2009-07-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sushou2009 的回复:]
引用 8 楼 king4323210 的回复:
SB

转义符号都不认识!


SB会的话就不问了!
[/Quote]
无语。。。。。。
笑的自然 2009-07-12
  • 打赏
  • 举报
回复

public static void main(String[] args) {

String path = "E:C\\company\\WebRoot\\images\\botton-cz.jpg";
System.out.println("Path:" + path);

String[] array = path.split("\\\\"); // 相当于两个\\ ,正则要求,仔细体会
System.out.println("File Name:" + array[array.length - 1]);

}
笑的自然 2009-07-11
  • 打赏
  • 举报
回复
思路:将路径字符串"E:\C\company\WebRoot\images\botton-cz.jpg"按"\"(在java中需要"\\"转义)分离字符串成字符串数组,去最后一个字符串元素也就是array[array.length - 1];

OK!
qq9791541 2009-07-11
  • 打赏
  • 举报
回复
int index = image.lastIndexOf('\'); 要用转意字符
ylx1989 2009-07-11
  • 打赏
  • 举报
回复
向页面中添加图片,我也经常出错的!
sushou2009 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 king4323210 的回复:]
SB

转义符号都不认识!
[/Quote]
SB会的话就不问了!
king4323210 2009-07-11
  • 打赏
  • 举报
回复
SB

转义符号都不认识!
sushou2009 2009-07-11
  • 打赏
  • 举报
回复
<%@ page language="java"
import="s2jsp.bysj.dao.*,s2jsp.bysj.entity.*,java.sql.*,java.util.*"
pageEncoding="gbk"%>
<%@ include file="checkSession.jsp"%>
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>管理员-添加</title>
<link rel="stylesheet" type="text/css" href="../css/style_admin.css">
<%
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gb2312");
String action = request.getParameter("action");
if (action != null && "save".equals(action)) {
String serialNumber = request.getParameter("serialNumber");
String name = request.getParameter("name");
String brand = request.getParameter("brand");
String model = request.getParameter("model");
String price = request.getParameter("price");
String image = request.getParameter("picture");
String description = request.getParameter("description");
int index = image.lastIndexOf("\\");
String picName = image.substring(index + 1);


Product product = new Product();

product.setSerialNumber(serialNumber);
product.setName(name);
product.setBrand(brand);
product.setModel(model);
product.setPrice(Double.parseDouble(price));
product.setPicture(picName);
product.setDescription(description);

ProductDao productDao = new ProductDao();

int count = productDao.addProduct(product);
if (count > 0)
out.print("<script type='text/javascript'>alert('成功添加一条商品信息。');location.replace('manageProduct.jsp');</script>");
else
out.print("<script type='text/javascript'>alert('添加失败。');history.go(-1);</script>");
}
%>
<script type="text/javascript">
function fun_check_form(){
if(document.form1.serialNumber.value==""){
alert("请输入商品编号(系列)信息信息。");
return false;
}else if(document.form1.name.value==""){
alert("请输入商品名称。");
return false;
}else if(document.form1.brand.value==""){
alert("请输入商品商标。");
return false;
}else if(document.form1.model.value==""){
alert("请输入商品型号。");
return false;
}else if(document.form1.price.value==""){
alert("请输入商品价格。");
return false;
if(!isNaN(document.form1.price.value)){
alert("");
return false;
}
}else{
return true;
}
}


function checkIsFloat(){
var nc=event.keyCode;
if(nc < 48 || nc > 57 ){
if(nc==46){
var s=document.form1.price.value;
for(var i=0;i<s.length;i++){
if(s.charAt(i)=='.'){
event.keyCode=0;
return;
}
}
}else{
event.keyCode=0;return;

}
}
}
</script>
</head>

<body>
<form name="form1" action="addProduct.jsp" method="post"
onSubmit="return fun_check_form()" ENCTYPE="multipart/form-data">
<table cellspacing="1" cellpadding="4" width="100%"
class="tableborder" id="table3">

<input type="hidden" name="action" value="save">
<tr>
<td colspan="12" class="header">
添加商品信息
</td>
</tr>
<tr>
<td class="altbg1">
商品编号(系列):
</td>
<td class="altbg2" colspan="11">
<input type="text" name="serialNumber" size="34">
</td>
</tr>
<tr>
<td class="altbg1">
商品名称:
</td>
<td class="altbg2" colspan="11">
<input type="text" name="name" size="34">
</td>
</tr>
<tr>
<td class="altbg1">
商品商标:
</td>
<td class="altbg2" colspan="11">
<input type="text" name="brand" size="34">
</td>
</tr>
<tr>
<td class="altbg1">
商品型号:
</td>
<td class="altbg2" colspan="11">
<input type="text" name="model" size="34">
</td>
</tr>
<tr>
<td class="altbg1">
商品价格:
</td>
<td class="altbg2" colspan="11">
<input type="text" name="price" size="34"
onkeypress="checkIsFloat();" />
</td>
</tr>
<tr>
<td class="altbg1">
商品图片:
</td>
<td class="altbg2" colspan="11">
<input type="file" name="picture" size="34">
</td>
</tr>
<tr>
<td class="altbg1">
商品介绍:
</td>
<td class="altbg2" colspan="11">
<textarea rows="5" cols="60" name="description"></textarea>
</td>
</tr>


<tr>
<td class="altbg1"></td>
<td class="altbg2" colspan="11">
<input type="submit" value="提交" name="B1" />
 
<input type="reset" value="重置" name="B2" />
</td>
</tr>

</table>
</form>
</body>

</html>
这是我这个页面的代码,红色就是问题的所在,添加总是不成功,为什么?那里有问题?求教各位大侠!
仙陀 2009-07-11
  • 打赏
  • 举报
回复
Java codeString str="E:\\C\\company\\WebRoo\\images\\botton-cz.jpg";intindex=str.lastIndexOf("\\");
System.out.println(str.substring(index+1));
输出:botton-cz.jpg


字符串长度的计算方法
提示:

(1)计算字符串长度时关键是要注意辨认转义字符;(2)一个转义字符总是以反斜杠开始,再加一个其他字符组成。所以,遇到反斜杠时要特别注意!!!

1、不带转义字符的字符串
  如:“abc!x=/”,其长度为 7

2、带转义字符的字符串

  (1) 字符串“abc\n”:其中的'\n'为转义字符(换行符),计算字符串长度时只能计作一个字符,所以该字符串的长度为4(而不是5)

  (2) 字符串“abc\n\\\'\"":其中有4个转义字符:'\n'(换行符)、'\\'(反斜杠)、'\''(单引号)、'\"'(双引号),所以该字符串的长度为7(而不是11)。
  (3) 字符串“abc\0xyz”:其中有一个转义字符'\0',它是字符串结束符,所以,当用函数strlen来测试该字符串的长度时,结果应该为3(而不是7)。

  (4) 字符串“abc\\0xy”:其中有一个转义字符'\\'(反斜杠),这样,后面的字符串“0xy”照样计算,所以,该字符串的长度为7(而不是将第二个反斜杠与其后的0结合为一个转义字符'\0',若那样的话,第一个反斜杠将无法处理,因为一个转义字符总是由反斜杠加其他字符组成的,单独的一个反斜杠不能作为任何合法的字符)。

  (5) 若将字符串“abc\\0xy”改为“abc\\\0xy”:则其中有二个转义字符'\\'(反斜杠)和'\0'(字符串结束符),这时,当用函数strlen来测试该字符串的长度时,结果应该为4(而不是7)。

  (6) 若将字符串“abc\\\0xy”改为“abc\\\061xy”:则其中有二个转义字符'\\'(反斜杠)和'\061'(ASCII码值等于061的字符,也即数字字符'1'),这时,当用函数strlen来测试该字符串的长度时,结果应该为7(而不是4或9)。所以,当遇到转义字符'\0'时,还要看其后面是否还有数字,若有,则应将后面的数字(一至二位)与前面的'\0'相结合作为一个字符计入整个字符串的长度。
     若用printf(“abc\\\061xy”);输出,则输出结果为:abc\1xy[/code]
sushou2009 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 closewbq 的回复:]
Java code
String str="E:\\C\\company\\WebRoo\\images\\botton-cz.jpg";
int index=str.lastIndexOf("\\");
System.out.println(str.substring(index+1));



输出:botton-cz.jpg
[/Quote]
为什么这里的\要改成\\呢?
zl3450341 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 closewbq 的回复:]
Java codeString str="E:\\C\\company\\WebRoo\\images\\botton-cz.jpg";intindex=str.lastIndexOf("\\");
System.out.println(str.substring(index+1));
输出:botton-cz.jpg
[/Quote]

正解
sushou2009 2009-07-11
  • 打赏
  • 举报
回复
int index = image.lastIndexOf('\');
为什么这里老是提示我\那里错误,换成“”也不对。
加载更多回复(2)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧