会乱码的原因是你的编码不一致导致的
php中的urlencode的编码是和系统编码一致的(比如windows默认gb2312,ubuntu默认utf-8)
所以首先需要确定你的系统编码,之后根据得到的系统编码在调用java的decode方法的时候,将这个编码传入(考虑到你的例子中有繁体字,所以,建议你使用utf-8编码),以下是我使用utf-8编码的例子(php环境是ubuntun下)
给你发个我以前的工具类吧、import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import javax.imageio.ImageIO
import sun.misc.BASE64Decoder
import sun.misc.BASE64Encoder
public class ImageChange {
/**
* 从path这个地址获取一张图片然后转为base64码
* @param imgName 图片的名字 如:123.gif(是带后缀的)
* @param path 123.gif图片存放的路径
* @return
* @throws Exception
*/
public static String getImageFromServer(String imgName,String path)throws Exception{
BASE64Encoder encoder = new sun.misc.BASE64Encoder()
File f = new File(path+imgName)
if(!f.exists()){
f.createNewFile()
}
BufferedImage bi = ImageIO.read(f)
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(bi, "gif", baos)
byte[] bytes = baos.toByteArray()
return encoder.encodeBuffer(bytes).trim()
}
/**
* 将一个base64转换成图片保存在 path 文件夹下 名为imgName.gif
* @param base64String
* @param path 是一个文件夹路径
* @param imgName 图片名字(没有后缀)
* @throws Exception
*/
public static String savePictoServer(String base64String,String path,String imgName)throws Exception{
BASE64Decoder decoder = new sun.misc.BASE64Decoder()
byte[] bytes1 = decoder.decodeBuffer(base64String)
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1)
BufferedImage bi1 =ImageIO.read(bais)
Date timeCur = new Date()
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy")
SimpleDateFormat fmtMM = new SimpleDateFormat("MM")
SimpleDateFormat fmtDD = new SimpleDateFormat("dd")
String strYY = fmtYY.format(timeCur)
String strMM = fmtMM.format(timeCur)
String strDD = fmtDD.format(timeCur)
String realPath = path+"/"+strYY+"/"+strMM+"/"+strDD
File dir=new File(realPath)
if(!dir.exists()){
dir.mkdirs()
}
String fileName=path+"\\"+strYY+"\\"+strMM+"\\"+strDD +"\\"+imgName+".gif"
File w2 = new File(fileName)//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2)//不管输出什么格式图片,此处不需改动
return fileName
}
public static void main(String[] args) throws Exception {
System.out.println(getImageFromServer("001001.gif","d:"))
}
}
你好,我写的BaseDao:package dao
import java.sql.*
/**
*
* @author Administrator
*数据库连接
*/
public class BaseDao {
//连接字符串
public String driver="oracle.jdbc.driver.OracleDriver"//数据库驱动
public String url="jdbc:oracle:thin:@localhost:1521:hfaccp"//建立到给定数据库 URL 的连接。
public String username="system"//数据库用户
public String password="system"//数据库密码
//声明接口
public Connection con
public PreparedStatement pstmt
public ResultSet rs
//获得数据库连接
public Connection getConnection()
{
try {
Class.forName(driver)
con=DriverManager.getConnection(url,username,password)
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
return con
}
//释放数据库资源
public void CloseAll()
{
if(rs!=null)
{
try {
rs.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if(pstmt!=null)
{
try {
pstmt.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if(con!=null)
{
try {
con.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
}
}