package get;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.serial.SerialBlob;
import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
public class Get {
public Blob f_b(File_ file){//fil_blob
String FILE="";
try {
InputStream InputStream=new FileInputStream(String.valueOf(file));
int bit;
while((bit=InputStream.read())!=-1){
FILE=FILE+Integer.toBinaryString(bit);
}
InputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buff = FILE.getBytes();
Blob blob = null;
try {
blob = new SerialBlob(buff);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return blob;
}
public Blob in_b(InputStream inputStream){//inputstreamתblob
String FILE="";
try {
InputStream InputStream=inputStream;
int bit;
while((bit=InputStream.read())!=-1){
FILE=FILE+Integer.toBinaryString(bit);
}
InputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buff = FILE.getBytes();
Blob blob = null;
try {
blob = new SerialBlob(buff);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return blob;
}
public It enform_getITEM(HttpServletRequest request) throws FileUploadException,IOException {//�õ��ϸ�ҳ����ı��ֶ�
It it=new It();
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
servletFileUpload.setHeaderEncoding("UTF-8");
List<FileItem> fileItem = servletFileUpload.parseRequest(request);
for(FileItem item:fileItem) {
String name_ = item.getFieldName();
if (item.isFormField()) {
String value = item.getString("UTF-8");
Text_ text=new Text_();
text.setFieldName(name_);
text.setValue(value);
it.t.add(text);
}
else {
InputStream in = item.getInputStream();
Blob b=this.in_b(in);
File_ file=new File_();
file.setFieldName(name_);
file.setValue(b);
it.f.add(file);
in.close();
}
}
return it;
}
public String get_text(String FieldName,It it){
for(Text_ the:it.t){//找出无法识别的原因
if(FieldName.equals(the.getFieldName())){
return the.getValue();
}
}
return "";
}
public Blob get_file(String FieldName,It it){
for(File_ the:it.f){
if(FieldName.equals(the.getFieldName())){
return the.getValue();
}
}
return null;
}
// public Blob enform_Blob_get(HttpServletRequest request, String get) throws IOException,FileUploadException {//�õ��ϸ�ҳ���ļ��ֶ�
// DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
// ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
// servletFileUpload.setHeaderEncoding("GBK");
// List<FileItem> fileItem = servletFileUpload.parseRequest(request);
// for(FileItem item:fileItem) {
// String name_ = item.getFieldName();
// if (!item.isFormField()) {
// if (get.equals(name_)) {
// InputStream in = item.getInputStream();
// Blob b=this.in_b(in);
// in.close();
// return b;
// }
// }
// }
// return null;
// }
}