📄 upload.java
字号:
package stumanage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import java.sql.*;
/**
* Upload:上传文件Servlet
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Upload
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try {
System.out.println("enter the first try"); //for test
DiskFileUpload fu = new DiskFileUpload();
/**
* 设置允许用户上传的文件最大值 单位:字节
*/
fu.setSizeMax(2*1024*1024);
/**
* 设置最多只允许在内存中的数据大小 单位:字节
*/
fu.setSizeThreshold(4096);
/**
* 设置不能放在内存中的数据临时存放的目录
*/
fu.setRepositoryPath("f:\\temp");
/**
* 开始读取上传信息
*/
System.out.println("信息设置成功");
List fileItems = fu.parseRequest(request);
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while(iter.hasNext()){
System.out.println("enter the while"); //for test
FileItem item = (FileItem)iter.next();
System.out.println(item.getName()); // for test
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
System.out.println(name);
long size = item.getSize();
if ( (name == null || name.equals("")) && size == 0)
continue;
try {
int byteRead = 0;
InputStream inStream = item.getInputStream();
InputStreamReader reader = new InputStreamReader(inStream);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dsn = "jdbc:odbc:info";
Connection con = DriverManager.getConnection(dsn);
String temp =
"insert into PaperInfo(Subject,PaperContent) values(?,?)";
PreparedStatement pstmt = con.prepareStatement(temp);
pstmt.setString(1, request.getParameter("subject"));
pstmt.setCharacterStream(2,reader,(int)size);
System.out.println(temp);
pstmt.executeUpdate();
System.out.println("上传成功");
inStream.close();
}
catch (Exception e) {
System.out.println("发生异常");
}
}
else {
throw new FileUploadException("fail to upLoad");
}
}
}catch(Exception e) {
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -