📄 servletupload.java
字号:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
import com.page.Show;
public class servletUpload extends HttpServlet {
private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
final public ServletConfig getServletConfig() {
return config;
}
/**
* Handles GET requests
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR><BR>");
out.println("The method of the HTML form must be POST.");
out.println("</BODY>");
out.println("</HTML>");
}
/**
* Handles POST requests
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 设置编码格式把浏览器送来的格式现转换为GB2312 同时把返回浏览器的数据格式设置为text/html;charset=GB2312
request.setCharacterEncoding("GB2312");
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
//HttpSession session = request.getSession(true);
Show show = new Show();
// Variables
//String upload = request.getParameter("path");
//System.out.println(upload);
int count = 0;
SmartUpload mySmartUpload = new SmartUpload();
com.jspsmart.upload.File file = null;
String filename = null;
try {
// Initialization
mySmartUpload.initialize(config, request, response);
// 限制每个上传文件的最大长度。
// mySmartUpload.setMaxFileSize(10000);
// 限制总上传数据的长度。
// mySmartUpload.setTotalMaxFileSize(20000);
// 设置允许上传的文件后缀
// mySmartUpload.setAllowedFilesList("tif,bmp,gif,jpg");
// 设置不允许上传的文件后缀
// mySmartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// Upload
mySmartUpload.upload();
// Save the file with the original name
// in a virtual path of the web server
//count = mySmartUpload.save("/upload", SmartUpload.SAVE_VIRTUAL);
String imagepath="c:\\ocr\\images";
//如果c盘目录下没此文件夹,则自动创建文件夹
java.io.File fileimage=new java.io.File(imagepath);
fileimage.mkdirs();
count = mySmartUpload.save("c:\\ocr\\images");
if (count == 0)
out.print(show.errorBox("请选择上传的文件", "提示"));
else {
// 获取上传文件的文件名
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
file = mySmartUpload.getFiles().getFile(i);
filename = file.getFileName();
if (file.isMissing())
out.print(show.errorBox("上传文件不存在", "提示"));
}
//把filename的名字存入session中,当用户下载文档后删除此文档和上传的图片
ServletContext session = getServletContext();
session.setAttribute("up", "已经上传");
session.setAttribute("filename", filename);
/*
* 在服务器建立存放文件目录 当文件上传成功后,进行识别工作,调用识别接口中的ident()方法 返回字符串 String
* result=ident(); 同时生成文本文件存入根目录下的ocrfile中 这里只生成一个和上传文件名相同的doc文件
*/
String path = "c:\\ocr\\file";
java.io.File filejdk = new java.io.File(path);
filejdk.mkdirs();
path = path+"\\"+filename+".txt";
java.io.File filejdk2 = new java.io.File(path);
filejdk2.createNewFile();
// filejdk.createTempFile(filename, ".doc");
// System.out.println("kaishil");
// Display the result
if (count != 0)
out.print(show.errorBox("文件上传成功", "提示"));
}
} catch (Exception e) {
out.print(show.errorBox("文件上传失败", "警告"));
e.printStackTrace();
}
// }
}
/**
* Destroy the servlet
*/
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -