mysql数据库在插入 带有“\”字符串的时候 数据库字段会屏蔽“\”例如 D:\wtpwebapps\LibraryManage\1.jpg时候 mysql数据库将屏蔽"\"-->""即变为D:wtpwebappsLibraryManage1.jpg 如果是需要打开该文件的路径,那么可以将D:\wtpwebapps\LibraryManage\1.jpg 转变为D:/wtpwebapps/LibraryManage/1.jpg 改路径同样可以打开该文件 所以在遇到改问题时候,可以在程序中 将"\"转变为"/" 但是同时又涉及到 \为转义字符,\'表示为一个',\"表示为",所以在程序设计的时候注意书书写代码:
public static final String changestr(String str){
str=str.replace('\\','/');
return str;
}
及可以将改路径插入到数据库中,
测试代码:新建一个字符转换类:public class ChStr {
public static final String changestr(String str){
str=str.replace('\\','/');
return str;
}
}
新建普通类
private ActionForward bookAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
filePath = request.getRealPath("\\") + "myPic\\";//取当前系统路径
filePath = new ChStr().changestr(filePath+ file.getFileName());
int a=bookDAO.insert(bookForm);}
新建dao层:
public class BookDAO {
public int insert(BookForm bookForm) {
sql = "Insert into tb_bookinfo (bfile) values(
' bookForm.getFilename()'
)
}
测试成功:
图:
bookForm.setFilename(filePath);
}
public static final String changestr(String str){
str=str.replace('\\','/');
return str;
}
及可以将改路径插入到数据库中,
测试代码:新建一个字符转换类:public class ChStr {
public static final String changestr(String str){
str=str.replace('\\','/');
return str;
}
}
新建普通类
private ActionForward bookAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
filePath = request.getRealPath("\\") + "myPic\\";//取当前系统路径
filePath = new ChStr().changestr(filePath+ file.getFileName());
int a=bookDAO.insert(bookForm);}
新建dao层:
public class BookDAO {
public int insert(BookForm bookForm) {
sql = "Insert into tb_bookinfo (bfile) values(
' bookForm.getFilename()'
)
}
测试成功:
图:
bookForm.setFilename(filePath);
}
本文介绍了在MySQL数据库中插入包含反斜杠的字符串路径时出现的问题,通过将反斜杠替换为正斜杠解决了路径解析错误,并提供了相应的代码示例。确保了正确插入和解析路径,以便于文件操作。
325

被折叠的 条评论
为什么被折叠?



