文件上传有两种情况 ①直接将文件存入数据库
/**通用文件上传(支持所有文件--直接写入数据库)
* @throws FileUploadException
* @throws IOException
* @throws SQLException **/
public Object commonFileUploadLoadInOracleBlob(HttpServletRequest request,
HttpServletResponse response, Connection conn)
throws FileUploadException, IOException, SQLException {
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(
diskFileItemFactory);
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
InputStream inputStream = null;
if (fileItemList != null) {
for (FileItem fileItem : fileItemList) {
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
if (name.equals("first_clum")) {// 非文件表单元素1
} else if (name.equals("second_clum")) {// 非文件表单元素2
}
}
if (!fileItem.isFormField()) {
inputStream = fileItem.getInputStream();
}
}
}
// 开始执行插入操作
String insettrTmgJgDlDetlSql = "insert into TR_TMG_PDA_BLOB "
+ "(FILE_BLOB,file_name) values(?,?)";
PreparedStatement prep = conn.prepareStatement(insettrTmgJgDlDetlSql);
prep.setBinaryStream(1, inputStream);
prep.setString(2, "fileName.jpg");
prep.addBatch();
prep.executeBatch();
prep.close();
return null;
}
② 将文件存入服务器
/**通用文件上传(文件上传到服务器)
* @throws Exception **/
public void commonFileUploadToService(HttpServletRequest request,
HttpServletResponse response, Connection conn, String uploadPath)
throws Exception {
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(
diskFileItemFactory);
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
File uploadedFile = null;
if (fileItemList != null) {
for (FileItem fileItem : fileItemList) {
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
if (name.equals("first_clum")) {// 非文件表单元素1
} else if (name.equals("second_clum")) {// 非文件表单元素2
}
}
if (!fileItem.isFormField()) {
String fileName = fileItem.getFieldName();
// 创建一个抽象路径,如果不存在的话就创建一个文件夹。
File dir = new File(uploadPath);
if (!dir.exists()) {
dir.mkdir();
}
String newFileFullName = uploadPath + fileName;
uploadedFile = new File(newFileFullName);
fileItem.write(uploadedFile);
}
}
}
}
文件下载代码如下
/**通用文件下载(直接下载到浏览器)
* @throws SQLException
* @throws IOException **/
public void commonFileDownloadFromOracleBlob(HttpServletRequest request,
HttpServletResponse response, Connection conn) throws SQLException,
IOException {
String sql = "";// 自己填写的sql
PreparedStatement prep = conn.prepareStatement(sql);
ResultSet rs = prep.executeQuery(sql);
while (rs.next()) {
String fileName = "";// 文件的名字
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("file_blob");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=\""
+ new String(fileName.getBytes("UTF-8"), "ISO8859-1")
+ "\"");
response.addHeader("extra_key_Name", fileName);
response.setHeader("Connection", "close");
response.setHeader("Content-Length", "");
response.addIntHeader("response_status", 2001);
response.addHeader("response_message", "ok");
addResponseHeader(response, 2001, "ok");
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = blob.getBinaryStream();
byte[] buffer = new byte[1024 * 1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
}