Base64解码。。。。。。。。。。。。。。。

<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.File"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">






<!--

-->
<%
String imageData2 = request.getParameter("imageData2");
String filePath="D:\";
String fileName="pthoto";
File file = new File(filePath+fileName);
try{

  FileOutputStream write = new FileOutputStream(file);
  byte[] decoderBytes = Base64.decode(imageData2);
  for(int i =0;i<decoderBytes.length;++i){
      if(decoderBytes[i]<0){//调整异常数据
          decoderBytes[i]+=256;

      }


  }
  write.write(decoderBytes);
  write.flush();
  write.close();

}catch(Exception e){
    e.printStackTrace();

}
%>这是代码
import java.io.UnsupportedEncodingException;

public class Base64 {
private static char[] base64EncodeChars = new char[] {

'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',

'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',

'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',

'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',

'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',

'o', 'p', 'q', 'r', 's', 't', 'u', 'v',

'w', 'x', 'y', 'z', '0', '1', '2', '3',

'4', '5', '6', '7', '8', '9', '+', '/' };

private static byte[] base64DecodeChars = new byte[] {   
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,   
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,   
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,   
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,   
-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,   
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,   
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,   
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };   

public static String encode(byte[] data) {   
    StringBuffer sb = new StringBuffer();   
    int len = data.length;   
    int i = 0;   
    int b1, b2, b3;   
    while (i < len) {   
        b1 = data[i++] & 0xff;   
        if (i == len)   
        {   
            sb.append(base64EncodeChars[b1 >>> 2]);   
            sb.append(base64EncodeChars[(b1 & 0x3) << 4]);   
            sb.append("==");   
            break;   
        }   
        b2 = data[i++] & 0xff;   
        if (i == len)   
        {   
            sb.append(base64EncodeChars[b1 >>> 2]);   
            sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);   
            sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);   
            sb.append("=");   
            break;   
        }   
        b3 = data[i++] & 0xff;   
        sb.append(base64EncodeChars[b1 >>> 2]);   
        sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);   
        sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);   
        sb.append(base64EncodeChars[b3 & 0x3f]);   
    }   
    return sb.toString();   
}   

public static byte[] decode(String str) throws UnsupportedEncodingException {   
    StringBuffer sb = new StringBuffer();   
    byte[] data = str.getBytes("UTF-8");   
    int len = data.length;   
    int i = 0;   
    int b1, b2, b3, b4;   
    while (i < len) {   
        /* b1 */   
        do {   
            b1 = base64DecodeChars[data[i++]];   
        } while (i < len && b1 == -1);   
        if (b1 == -1) break;   
        /* b2 */   
        do {   
            b2 = base64DecodeChars[data[i++]];   
        } while (i < len && b2 == -1);   
        if (b2 == -1) break;   
        sb.append((char)((b1 << 2) | ((b2 & 0x30) >>> 4)));   
        /* b3 */   
        do {   
            b3 = data[i++];   
            if (b3 == 61) return sb.toString().getBytes("UTF-8");   
            b3 = base64DecodeChars[b3];   
        } while (i < len && b3 == -1);   
        if (b3 == -1) break;   
        sb.append((char)(((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));   
        /* b4 */   
        do {   
            b4 = data[i++];   
            if (b4 == 61) return sb.toString().getBytes("UTF-8");   
            b4 = base64DecodeChars[b4];   
        } while (i < len && b4 == -1);   
        if (b4 == -1) break;   
        sb.append((char)(((b3 & 0x03) << 6) | b4));   
    }   
    return sb.toString().getBytes("UTF-8");   
}   

}

这是base64
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 28 in the jsp file: /save.jsp
Base64 cannot be resolved
25: try{
26:

27: FileOutputStream write = new FileOutputStream(file);
28: byte[] decoderBytes = Base64.decode(imageData2);
29: for(int i =0;i<decoderBytes.length;++i){
30: if(decoderBytes[i]<0){//璋冩暣寮傚父鏁版嵁
31: decoderBytes[i]+=256;

Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2516)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2505)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)
这是报错信息求解答
我知道的原因是数据转换问题但是不知道怎么解决

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!