📄 uploadbean.java
字号:
package uploadImageBean;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import Image.ImageAnnotation;
public class uploadBean {
static {
System.loadLibrary("imgAnotation");
}
public static ImageAnnotation imageAnnotation = new ImageAnnotation(
"c:\\model.txt");
private InputStream InputStreamState;
private PageContext pageContext;
private long fileSize;
private String errorMessage = "";
private HttpServletRequest request;
private Map<String, String> Parameter = new HashMap<String, String>();
public uploadBean() {
}
/**
* <pre><h1>
* 解析Form表单
* </h1>
*
* form表单的enctype必须设置为:enctype="multipart/form-data"
* </br>
* 此方法为首方法,用于解析form表单中的属性。执行此方法解析表单值后,方可通过 {@link #getParameter(String)}方法获得表单的参数信息
* <br>
*
* @param contentType
* 传入内容类型信息。例如:request.getContentType();
* @param inputStream
* 传入请求的输入流。例如:request.getInputStream();
* </pre>
*/
private void resolverForm() {
InputStream inputStream = null;
try {
inputStream = pageContext.getRequest().getInputStream();
} catch (IOException e2) {
e2.printStackTrace();
}
String contentType = pageContext.getRequest().getContentType();
if (!isMultipar(contentType) || inputStream.equals(InputStreamState))
return;
if (contentType != null && inputStream != null) {
InputStreamState = inputStream;
int data;
StringBuffer datastr = new StringBuffer();
Parameter.clear();
try {
while ((data = inputStream.read()) != -1) {
datastr.append((char) data);
if (fileSize > 0 && datastr.length() > fileSize) {
datastr = null;
errorMessage = "文件超出大小限制。";
Parameter.put("error", errorMessage);
throw new Exception("文件超出大小限制。");
} else
Parameter.put("error", errorMessage);
}
inputStream.close();
String split = "boundary=";
String splitStr = "--"
+ contentType.substring(contentType.indexOf(split)
+ split.length());
String[] formFileds = datastr.toString().split(
"Content-Disposition: form-data; ");
for (int i = 0; i < formFileds.length; i++) {
int[] index = new int[4];
if (!formFileds[i].startsWith(splitStr)) {
index[0] = -1;
index[1] = formFileds[i].indexOf("\n", index[0]);
index[2] = formFileds[i].indexOf("\n", index[1] + 1);
index[3] = formFileds[i].indexOf("\n", index[2] + 1);
String name = "";
for (int lc = 0; lc < index.length - 1; lc++) {
String line = formFileds[i].substring(
index[lc] + 1, index[lc + 1]);
String[] lineFields = line.split("; ");
for (int j = 0; j < lineFields.length; j++) {
if (lineFields[j].startsWith("name=")) {
name = lineFields[j].substring(
lineFields[j].indexOf("\"") + 1,
lineFields[j].lastIndexOf("\""));
}
if (j > 0) {
String arg = name
+ "_"
+ lineFields[j].substring(0,
lineFields[j].indexOf("="));
String argContent = lineFields[j]
.substring(lineFields[j]
.indexOf("\"") + 1,
lineFields[j]
.lastIndexOf("\""));
Parameter.put(arg, argContent);
}
}
if (line.equals("\r")) {
Parameter.put(name, formFileds[i].substring(
index[lc + 1] + 1, formFileds[i]
.lastIndexOf(splitStr) - 2));
break;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* <pre><h1>
* 上传文件到服务器
* </h1>
*
* @param filePath
* 服务器存放文件的路径
* </pre>
*/
public boolean uploadToFile(String filearg, String filePath) {
// File temp=
String filename = System.currentTimeMillis()
+ new File(getParameter(filearg + "_filename")).getName();
/* 为了显示图片 */
String absolutePath = filePath + filename;
//request.setAttribute("absolutePath", absolutePath);
request.setAttribute("absolutePath", "..\\upload\\"+filename);
String contentType = pageContext.getRequest().getContentType();
if (!isMultipar(contentType))
return false;
resolverForm();
if (filePath.charAt(filePath.length() - 1) != File.separatorChar)
filePath += File.separatorChar;
String submit = getParameter("Submit");
String file = getParameter(filearg);
if (submit != null && file != null) {
try {
File newfile = new File(new String((filePath + new File(
filename).getName()).getBytes("iso-8859-1")));
newfile.createNewFile();
FileOutputStream fout = new FileOutputStream(newfile);
fout.write(file.getBytes("iso-8859-1"));
fout.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String result = imageAnnotation.annotate(absolutePath);
request.setAttribute("result", result);
return true;
}
return false;
}
/**
* <pre><h1>
* 获取表单的指定参数
* </h1>
* 必须调用 {@link #resolverForm(String, InputStream)}方法解析form表单以后,才可以调用此方法,否则参数为空。
*
* @param param
* 参数名称
* @return String 返回类型
* </pre>
*/
public String getParameter(String param) {
resolverForm();
return Parameter.get(param);
}
/**
* <pre><h1>
* 获得form表单的所有参数名称的集合
* </h1>
*
* @return java.util.Set
* </pre>
*/
public Set<String> getParameterNames() {
resolverForm();
return Parameter.keySet();
}
private boolean isMultipar(String contentType) {
try {
if (contentType != null
&& !contentType.startsWith("multipart/form-data")) {
throw new Exception(
"请设置Form表单的enctype属性为:\"multipart/form-data\"");
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* <pre><h1>
* 设置上下文
* </h1>
* @param pageContext javax.servlet.jsp.PageContext类型的上下文,
* 可以直接传入JSP页面中的pageContext内置对象。
* </pre>
*/
public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -