J_Howie 2017-11-01 07:46 采纳率: 0%
浏览 920
已结题

上传文件到阿里云,项目架构限制,用servlet方法上传,但是现在的文件不对,处理代码如下:

package com.twsz.creative.lucy.controller;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpRetryException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;

import com.howie.transitionService.UploadService;

@Controller
public class UploadServlet extends HttpServlet {

private UploadService uploadService;

public void service(HttpServletRequest request,HttpServletResponse response) throws HttpRetryException,IOException, ServletException{
    uploadService = new UploadService();
    byte[] body = readBody(request);
    String textBody = new String(body,"UTF-8");
    String fileName = getFileName(textBody);    //获取到文件名
    int[] position = getFilePosition(request,textBody);
    int length = position[1] - position[0];
    byte[] bytes = new byte[length+1];
    for(int i=0;i < length;i++){
        bytes[i] = body[position[0]];
        position[0] +=1;
    }
    //System.arraycopy(body, position[0], bytes, 0, position[1]-position[0]+1); 
    /* 截取的时候老是出现数组越界的情况
     * byte[] bytes = subBytes(body,getFilePosition(request,textBody)[0],
            getFilePosition(request,textBody)[1]);*/
    System.out.println(fileName);
    System.out.println("原byte流:"+body);
    System.out.println("文件byte流:");
    System.out.println(bytes);
    String url = uploadService.uploadFile(bytes, fileName);
    System.out.println(url);
}

/*private byte[] requestToByte(HttpServletRequest request) throws IOException, ServletException{
    Part part = request.getPart("myfile");
    String disposition = part.getHeader("content-dispositon");
    String fileName = disposition.substring(disposition.lastIndexOf("=")+2,disposition.length()-1);
    InputStream is = part.getInputStream();
    String path = getServletContext().getRealPath("/upload");
    FileOutputStream out = new FileOutputStream(path+fileName);
    byte[] buf = new byte[10*1024*1024];
    int len = 0;
    while((len=is.read(buf)) != -1){
        out.write(buf, 0, len);
    }
    out.close();
    return buf;
}*/

//将request转化成byte[]格式
private byte[] readBody(HttpServletRequest request) throws IOException{
    int formDataLength = request.getContentLength();
    DataInputStream dataStream = new DataInputStream(request.getInputStream());
    byte body[] = new byte[formDataLength];
    int totalBytes = 0;
    while(totalBytes < formDataLength){
        int bytes = dataStream.read(body, totalBytes, formDataLength);
        totalBytes += bytes;
    }
    return body;
}

//返回文件名
private String getFileName(String repBody){
    String fileName = repBody.substring(repBody.indexOf("filename=\"")+10);
    fileName = fileName.substring(0,fileName.indexOf("\n"));
    fileName = fileName.substring(fileName.lastIndexOf("\\")+1,
            fileName.indexOf("\""));
    return fileName;
} 

//从byte[]流中去除http头部,截取文件的部分
private byte[] subBytes(byte[] src,int begin,int end){
    int length  = end-begin + 1;
    byte[] bytes = new byte[length];
    System.arraycopy(src, begin, bytes, 0, length-1);
    return bytes;
}

private int[] getFilePosition(HttpServletRequest request,String textBody) throws UnsupportedEncodingException{
    String contentType = request.getContentType();
    String boundaryText = contentType.substring(
            contentType.lastIndexOf("=")+1,contentType.length());
    int pos = textBody.indexOf("filename=\"");
    System.out.println(textBody.charAt(pos));
    pos = textBody.indexOf("\n", pos) + 1;
    System.out.println(textBody.charAt(pos));
    pos = textBody.indexOf("\n", pos) + 1;
    System.out.println(textBody.charAt(pos));
    pos = textBody.indexOf("\n", pos) + 1;
    System.out.println(textBody.charAt(pos));
    int boundaryLoc = textBody.indexOf(boundaryText, pos) - 4;
    System.out.println(textBody.charAt(boundaryLoc));
    int begin = ((textBody.substring(0,pos)).getBytes("utf-8")).length;
    int end = ((textBody.substring(0,boundaryLoc)).getBytes("utf-8")).length;
    int[] position = new int[2];        //将文件的位置存放在position里
    position[0] = begin;
    position[1] = end;
    return position;
}

}

  • 写回答

2条回答 默认 最新

  • devmiao 2017-11-01 15:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作