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;
}
}