81,092
社区成员
发帖
与我相关
我的任务
分享
public void FileUpload(HttpServletRequest request, HttpServletResponse response, String uuid)
throws Exception {
Connection conn = null;
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(ConstantFactory.getConstant("HSRDB"));
conn = ds.getConnection();
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
FileItem item = null;
while (iter.hasNext()) {
item = (FileItem) iter.next();
if (!item.isFormField()) {
AttachmentInfo attachmentInfo = new AttachmentInfo();
String fileName = item.getName();
String shortFileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
attachmentInfo.setFileName(shortFileName);
attachmentInfo.setBizDataUuid(uuid);
InputStream stream = item.getInputStream();
byte[] bytes = new byte[1024 * 1024];
int leng = stream.read(bytes);
if (leng != -1) {
byte[] bytes1 = new byte[leng];
for (int i = 0; i < leng; i++) {
bytes1[i] = bytes[i];
}
CommonMethod.createInstance(attachmentInfo);
PreparedStatement pstmt = conn
.prepareStatement("UPDATE AttachmentInfo SET imgData = ? where AttachmentInfo.uuid = '"
+ attachmentInfo.getUuid() + "'");
pstmt.setBytes(1, bytes1);
pstmt.executeUpdate();
if (pstmt != null) {
pstmt.close();
}
}
}
}
}
catch (Exception e) {
System.out.println(e.getStackTrace());
}
finally {
if (conn != null) {
conn.close();
}
}
}