String index out of range: -1超出索引?怎么办?

陽。 2016-06-26 04:13:30
页面实现修改图像的代码出错



int count=p_image.indexOf("images");
String p_image_temp=p_image.substring(count,p_image.length());




修改后不会出现错误,当时无法识别路径了


int count=p_image.indexOf("images");
String p_image_temp=p_image.substring(count+1,p_image.length());




该怎么办???
...全文
16779 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
家里敷泥呀 2016-06-27
  • 打赏
  • 举报
回复
你也知道是这两行代码出错了。 int count=p_image.indexOf("images"); String p_image_temp=p_image.substring(count,p_image.length()); java.lang.StringIndexOutOfBoundsException: String index out of range: -1 很明显是count=-1了,字符串从-1开始截取自然会抛异常。 注意这里的-1是表示没有找到"images"这个字符串,而不是说找到了,位置在-1处。 所以加个判断就ok了 String p_image_temp=p_image; if(count>-1){ p_image_temp=p_image.substring(count,p_image.length()); }
  • 打赏
  • 举报
回复
改数据库路径,改图片位置
  • 打赏
  • 举报
回复
引用 8 楼 qq421813163 的回复:
[quote=引用 7 楼 qnmdcsdn 的回复:] [quote=引用 5 楼 qq421813163 的回复:] [quote=引用 2 楼 qnmdcsdn 的回复:] 改数据库路径,改图片位置
楼下修正了错误,可是还是无法识别绝对路径,能帮忙看看吗[/quote] 同问,目前描述看的不是太懂,哪里无法识别路径[/quote]
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="org.fjzzy.eBuy.classes.ConnDB" %>
<%@ page import="java.sql.*" %>
<% 
request.setCharacterEncoding("UTF-8");
	String admin_name=(String) session.getAttribute("admin_name");
	if(admin_name==null || admin_name=="")
	{
		out.println("<script language='javascript'>alert('请先登录!');window.location.href='index.jsp';</script>");
	}
	String p_id=(String) request.getParameter("p_id");
	String p_type="",p_name="",p_price="",p_quantity="",p_image="",p_description="";
	ConnDB conn=new ConnDB();
	String sql="select * from product where p_id='"+p_id+"'";
	ResultSet rs = conn.doQuery(sql);
	if(rs.next())
	{
		p_type=rs.getString(1);
		p_name=rs.getString(3);
		p_price=rs.getString(4);
		p_quantity=rs.getString(5);
		p_image=rs.getString(6);
		p_description=rs.getString(7);
	}
	rs.close();
	

 %>
<style type="text/css">
<!--
.STYLE1 {
	font-size: 12px;
}
.STYLE2 {
	font-size: 14px;
}

-->
</style>
<script>
	<!--
	function check_product(form)
	{
		if(form.p_type.value=="")
		{
			alert("请选择商品类型");
			form.p_type.focus();
			return false;
		}
		if(form.p_id.value=="")
		{
			alert("请输入商品的ID");
			form.p_id.focus();
			return false;
		}	
		if(form.p_name.value=="")
		{
			alert("请输入商品的名称");
			form.p_name.focus();
			return false;
		}	
		if(form.p_price.value=="")
		{
			alert("请输入商品的价格");
			form.p_price.focus();
			return false;
		}	
		if(form.p_quantity.value=="")
		{
			alert("请输入商品的数量");
			form.p_quantity.focus();
			return false;
		}	
		if(form.p_image.value=="")
		{
			alert("请添加商品的图片");
			form.p_image.focus();
			return false;
		}	
	}
	
	-->
</script>
<center>
<form action="product_update_ok.jsp?p_id=<%= p_id %>" method="post" onSubmit="return check_product(this);">
<table width="520" border="1" bordercolor="#99CCFF" cellpadding="0" cellspacing="0" style="border-collapse:collapse">
  <!--DWLayoutTable-->
  <tr>
    <td height="30" colspan="2" align="center" bgcolor="#FFFFFF"><span class="STYLE2">查看/修改商品信息</span></td>
    </tr>
  <tr>
    <td width="146" height=40 align="center"><span class="STYLE1">商品类型</span>:</td>
    <td width="348" align="left">  
      <select name="p_type">
	  <option value="<%= p_type %>"><%= p_type %></option>
	  <%
		String str_sql="select t_type from main_type where t_type != '"+p_type+"'";
		rs=conn.doQuery(str_sql);
		while(rs.next())
		{
	  %>
	  	<option value="<%= rs.getString("t_type") %>"><%= rs.getString("t_type") %></option>
	  <%}%>
    </select></td>
  </tr>
  <tr>
    <td height="30" align="center"><span class="STYLE1">商品ID</span>:</td>
    <td height="30" align="left">  
      <input type="text" name="p_id" size="15" value="<%= p_id %>"/></td>
  </tr>
  <tr>
    <td height="30" align="center"><span class="STYLE1">商品名称</span>:</td>
    <td height="30" align="left">  
      <input type="text" name="p_name" size="30" value="<%= p_name %>"  /></td>
  </tr>
  <tr>
    <td height="30" align="center"><span class="STYLE1">商品价格( RMB )</span>:</td>
    <td height="30" align="left">  
      <input type="text" name="p_price" size="10" value="<%= p_price %>" /></td>
  </tr>
  <tr>
    <td height="30" align="center"><span class="STYLE1">商品数量</span>:</td>
    <td height="30" align="left">  
      <input type="text" name="p_quantity" size="10" value="<%= p_quantity %>"  /></td>
  </tr>
  <tr>
    <td height="30" align="center"><span class="STYLE1">商品图片</span>:</td>
    <td height="30" align="left">  
      <input type="file" name="p_image" size="30" value="<%= p_image %>"/></td>
  </tr>
  
  <tr>z
    <td height="94" align="center"><span class="STYLE1">商品描述</span>:</td>
    <td height="94" align="left">  
      <textarea name="p_description" cols="42" rows="5"><%= p_description %></textarea></td>
  </tr>
  <tr>
    <td height="40" colspan="2" align="center"><input name="submit" type="submit" class="STYLE1" value=" 修改 " />        
      <input type="button" class="STYLE1" value=" 返回 " onclick="history.go(-1);"/></td> 
  </tr>
</table>
</form>
</center>
这是页面前台代码,上面那个是后台更新代码 效果图如下: [/quote] 先看看上边得到值了没有,得到的是什么,再看下边显示的问题,要一步步排查
陽。 2016-06-27
  • 打赏
  • 举报
回复
引用 7 楼 qnmdcsdn 的回复:
[quote=引用 5 楼 qq421813163 的回复:]
[quote=引用 2 楼 qnmdcsdn 的回复:]
改数据库路径,改图片位置



楼下修正了错误,可是还是无法识别绝对路径,能帮忙看看吗[/quote]

同问,目前描述看的不是太懂,哪里无法识别路径[/quote]


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="org.fjzzy.eBuy.classes.ConnDB" %>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("UTF-8");
String admin_name=(String) session.getAttribute("admin_name");
if(admin_name==null || admin_name=="")
{
out.println("<script language='javascript'>alert('请先登录!');window.location.href='index.jsp';</script>");
}
String p_id=(String) request.getParameter("p_id");
String p_type="",p_name="",p_price="",p_quantity="",p_image="",p_description="";
ConnDB conn=new ConnDB();
String sql="select * from product where p_id='"+p_id+"'";
ResultSet rs = conn.doQuery(sql);
if(rs.next())
{
p_type=rs.getString(1);
p_name=rs.getString(3);
p_price=rs.getString(4);
p_quantity=rs.getString(5);
p_image=rs.getString(6);
p_description=rs.getString(7);
}
rs.close();


%>
<style type="text/css">
<!--
.STYLE1 {
font-size: 12px;
}
.STYLE2 {
font-size: 14px;
}

-->
</style>
<script>
<!--
function check_product(form)
{
if(form.p_type.value=="")
{
alert("请选择商品类型");
form.p_type.focus();
return false;
}
if(form.p_id.value=="")
{
alert("请输入商品的ID");
form.p_id.focus();
return false;
}
if(form.p_name.value=="")
{
alert("请输入商品的名称");
form.p_name.focus();
return false;
}
if(form.p_price.value=="")
{
alert("请输入商品的价格");
form.p_price.focus();
return false;
}
if(form.p_quantity.value=="")
{
alert("请输入商品的数量");
form.p_quantity.focus();
return false;
}
if(form.p_image.value=="")
{
alert("请添加商品的图片");
form.p_image.focus();
return false;
}
}

-->
</script>
<center>
<form action="product_update_ok.jsp?p_id=<%= p_id %>" method="post" onSubmit="return check_product(this);">
<table width="520" border="1" bordercolor="#99CCFF" cellpadding="0" cellspacing="0" style="border-collapse:collapse">
<!--DWLayoutTable-->
<tr>
<td height="30" colspan="2" align="center" bgcolor="#FFFFFF"><span class="STYLE2">查看/修改商品信息</span></td>
</tr>
<tr>
<td width="146" height=40 align="center"><span class="STYLE1">商品类型</span>:</td>
<td width="348" align="left">  
<select name="p_type">
<option value="<%= p_type %>"><%= p_type %></option>
<%
String str_sql="select t_type from main_type where t_type != '"+p_type+"'";
rs=conn.doQuery(str_sql);
while(rs.next())
{
%>
<option value="<%= rs.getString("t_type") %>"><%= rs.getString("t_type") %></option>
<%}%>
</select></td>
</tr>
<tr>
<td height="30" align="center"><span class="STYLE1">商品ID</span>:</td>
<td height="30" align="left">  
<input type="text" name="p_id" size="15" value="<%= p_id %>"/></td>
</tr>
<tr>
<td height="30" align="center"><span class="STYLE1">商品名称</span>:</td>
<td height="30" align="left">  
<input type="text" name="p_name" size="30" value="<%= p_name %>" /></td>
</tr>
<tr>
<td height="30" align="center"><span class="STYLE1">商品价格( RMB )</span>:</td>
<td height="30" align="left">  
<input type="text" name="p_price" size="10" value="<%= p_price %>" /></td>
</tr>
<tr>
<td height="30" align="center"><span class="STYLE1">商品数量</span>:</td>
<td height="30" align="left">  
<input type="text" name="p_quantity" size="10" value="<%= p_quantity %>" /></td>
</tr>
<tr>
<td height="30" align="center"><span class="STYLE1">商品图片</span>:</td>
<td height="30" align="left">  
<input type="file" name="p_image" size="30" value="<%= p_image %>"/></td>
</tr>

<tr>z
<td height="94" align="center"><span class="STYLE1">商品描述</span>:</td>
<td height="94" align="left">  
<textarea name="p_description" cols="42" rows="5"><%= p_description %></textarea></td>
</tr>
<tr>
<td height="40" colspan="2" align="center"><input name="submit" type="submit" class="STYLE1" value=" 修改 " />   
<input type="button" class="STYLE1" value=" 返回 " onclick="history.go(-1);"/></td>
</tr>
</table>
</form>
</center>



这是页面前台代码,上面那个是后台更新代码
效果图如下:
  • 打赏
  • 举报
回复
引用 5 楼 qq421813163 的回复:
[quote=引用 2 楼 qnmdcsdn 的回复:] 改数据库路径,改图片位置
楼下修正了错误,可是还是无法识别绝对路径,能帮忙看看吗[/quote] 同问,目前描述看的不是太懂,哪里无法识别路径
家里敷泥呀 2016-06-27
  • 打赏
  • 举报
回复
引用 4 楼 qq421813163 的回复:
[quote=引用 3 楼 Q80470101 的回复:] 你也知道是这两行代码出错了。 int count=p_image.indexOf("images"); String p_image_temp=p_image.substring(count,p_image.length()); java.lang.StringIndexOutOfBoundsException: String index out of range: -1 很明显是count=-1了,字符串从-1开始截取自然会抛异常。 注意这里的-1是表示没有找到"images"这个字符串,而不是说找到了,位置在-1处。 所以加个判断就ok了 String p_image_temp=p_image; if(count>-1){ p_image_temp=p_image.substring(count,p_image.length()); }
修改后错误确实没了,可是还是无法识别路径,怎么办? 把(长虹888)图片路径改成(C:\software\eclipse\workspace\eBuy\WebContent\images\TV\5.jpg) (长虹888)把上传路径改成(C:\Users\pgy\Desktop\222.jpg) 还是无法识别 [/quote] 我没看懂你的意思。 浏览器端上传文件,处于用户安全性考虑,是取不到上传的真实路径的。
陽。 2016-06-27
  • 打赏
  • 举报
回复
引用 2 楼 qnmdcsdn 的回复:
改数据库路径,改图片位置
楼下修正了错误,可是还是无法识别绝对路径,能帮忙看看吗
陽。 2016-06-27
  • 打赏
  • 举报
回复
引用 3 楼 Q80470101 的回复:
你也知道是这两行代码出错了。
int count=p_image.indexOf("images");
String p_image_temp=p_image.substring(count,p_image.length());

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

很明显是count=-1了,字符串从-1开始截取自然会抛异常。
注意这里的-1是表示没有找到"images"这个字符串,而不是说找到了,位置在-1处。
所以加个判断就ok了
String p_image_temp=p_image;
if(count>-1){
p_image_temp=p_image.substring(count,p_image.length());
}



修改后错误确实没了,可是还是无法识别路径,怎么办?

把(长虹888)图片路径改成(C:\software\eclipse\workspace\eBuy\WebContent\images\TV\5.jpg)



(长虹888)把上传路径改成(C:\Users\pgy\Desktop\222.jpg)



还是无法识别


陽。 2016-06-26
  • 打赏
  • 举报
回复
整个页面代码(实现更新图片)
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="org.fjzzy.eBuy.classes.ConnDB" %>
<%@ page import="org.fjzzy.eBuy.classes.Convert" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<% 
request.setCharacterEncoding("UTF-8");
String admin_name=(String) session.getAttribute("admin_name");
	if(admin_name==null || admin_name=="")
	{
		out.println("<script language='javascript'>alert('请先登录!');window.location.href='index.jsp';</script>");
	}
	else
	{
	Date date=new Date();
	String p_id=(String) request.getParameter("p_id");
	String p_type=(String) request.getParameter("p_type");
	String p_name=(String) request.getParameter("p_name");
	String p_price=(String) request.getParameter("p_price");
	String p_quantity=(String) request.getParameter("p_quantity");
	String p_image=(String) request.getParameter("p_image");
	p_image=p_image.replaceAll("\\\\","/");
	int count=p_image.indexOf("images");
	String p_image_temp=p_image.substring(count,p_image.length());
	
	String p_description=(String) request.getParameter("p_description");
	String p_time=String.valueOf(date.getMonth()+1)+"-"+date.getDate()+"-20"+String.valueOf(date.getYear()).substring(1);	
	
	ConnDB conn=new ConnDB();
	Convert convert=new Convert();
	p_type=convert.method(p_type);
	p_id=convert.method(p_id);
	p_name=convert.method(p_name);
	p_image_temp=convert.method(p_image_temp);
	p_description=convert.method(p_description);
	
		String sql="update product set p_type='"+p_type+"',p_name='"+p_name+"',p_price='"+Float.parseFloat(p_price)+"',p_quantity='"+Integer.parseInt(p_quantity)+"',p_image='"+p_image_temp+"',p_description='"+p_description+"',p_time='"+p_time+"' where p_id='"+p_id+"'";
	int temp=conn.doUpdate(sql);
	if(temp!=0)
	{
		out.println( "<HTML><HEAD><META http-equiv='refresh' content='2; URL=product_all.jsp' target=Main></HEAD><BODY bgcolor='#FFFFFF'></body></html>");
		%>
			<jsp:include page="update_ok.html" flush="true"/>
		<%
	}
	else
	{
		out.println( "<HTML><HEAD><META http-equiv='refresh' content='2; URL=product_all.jsp' target=Main></HEAD><BODY bgcolor='#FFFFFF'><center><font size=4pt color='red'>" + "商品修改失败!" + "</font></center></body></html>");
	}
}
 %>
错误代码
2016-6-26 15:19:01 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(Unknown Source)
	at org.apache.jsp.admin.product_005fupdate_005fok_jsp._jspService(product_005fupdate_005fok_jsp.java:80)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
	at java.lang.Thread.run(Unknown Source)

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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