📄 uploadservlet.java
字号:
package com.mobile.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.mobile.dao.TMobileDAO;
public class UploadServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
//这里,我们需要实现一个文件上传的效果
//声明Smartupload对象
SmartUpload su = new SmartUpload();
//传入相关参数,完成smartupload初始化
su.initialize(this.getServletConfig(), request, response);
TMobileDAO tb=new TMobileDAO();
//上传文件
try{
//限定上传文件的类型
//su.setAllowedFilesList("txt,dll,doc");
su.setDeniedFilesList("txt");
//相当于读取文件流
su.upload();
Request req = su.getRequest();
String str = req.getParameter("myname");
System.out.println(str);
Files files = su.getFiles();
int count = files.getCount();
//循环对每一个文件进行处理
for(int i=0;i<count;i++){
//得到每一个文件
File f = files.getFile(i);
if(!f.isMissing()){
f.saveAs("e:/resource/"+f.getFileName(),File.SAVEAS_VIRTUAL);
String mypath="e:/resource/"+f.getFileName();
tb.inputNumFromFile(mypath);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -