📄 uploadhtmls.jsp
字号:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.io.*,java.sql.*,java.lang.Math.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String contextPath = request.getContextPath();
String infoid = request.getParameter("infoid");
// Initialization
mySmartUpload.initialize(pageContext);
// Upload
mySmartUpload.upload();
//{*[Cancle]*}注释以保{*[Save file]*}
//mySmartUpload.save("C:/jboss/server/default/deploy/webroot.war/uploads/info");
//String infoid = mySmartUpload.getRequest().getParameter("infoid");
String docid;
String filename;
String data;
long filesize;
com.jspsmart.upload.File myFile = null;
String temphtmlpath = "C:/jboss/server/default/deploy/webroot.war/uploads/info/"+infoid;
//{*[Creat the temp directory]*}
java.io.File tdir = new java.io.File(temphtmlpath);
if (!tdir.isDirectory()) {
tdir.mkdir();
}
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
myFile = mySmartUpload.getFiles().getFile(i);
//debug only
System.out.println("File=" + myFile.getFileName());
//{*[Handle file and data]*}
if (!myFile.isMissing())
{
filename = myFile.getFileName();
filesize = myFile.getSize();
System.out.println("myFile.getFieldName()="+myFile.getFieldName());
if(myFile.getFieldName().equalsIgnoreCase("HTMLFILES") ) //HTML文件
{
System.out.println("处理HTML文件");
try
{
//将文件{*[Save]*}到临时目录
System.out.println(temphtmlpath+"/"+filename);
myFile.saveAs(temphtmlpath+"/"+filename);
out.println("{*[The saved file]*}: " + filename + "<br>");
out.println("大小: " + filesize + " bytes<br>");
}
catch(Exception e)
{
out.println("{*[Error occurred]*}: " + e.toString());
}
}
else //其他{*[Attachment]*}文件.本示例中因为在{*[Save]*}为html时没有提交form数据,不会执行
{
try
{
//{*[Add data into attachment]*}
System.out.println("{*[Handle attachment]*}");
out.println("{*[The saved file]*}: " + filename + "<br>");
out.println("大小: " + filesize + " bytes<br>");
}
catch(Exception e)
{
out.println("{*[Error occurred]*}: " + e.toString());
}
}
} // end if (!myFile.isMissing())
}// end for
//处理文件
java.io.File allfiles[] = tdir.listFiles();
java.io.File HTMLFile = null;
int HTMLFileSize = 0;
java.io.File OtherFile = null;
System.out.println("寻找html文件");
//寻找html文件
for (int i=0;i< allfiles.length; i++)
{
if( allfiles[i].isFile() )
{
if ( allfiles[i].getName().endsWith(".htm"))
{
HTMLFile = new java.io.File(temphtmlpath+"/"+allfiles[i].getName());
HTMLFileSize = (int)allfiles[i].length();
System.out.println(allfiles[i].getName());
}
}
}
if (HTMLFile != null)
{
InputStream inStream=new java.io.FileInputStream(HTMLFile);
byte[] bytes = new byte[HTMLFileSize];
inStream.read(bytes);
inStream.close();
String htmlcontent = new String(bytes);
//将每一个不{*[Yes]*}HTML的文件添加到数据库,并{*[Modify]*}HTML文件中有关其连接的{*[Info]*}
for (int i=0;i< allfiles.length; i++)
{
if( allfiles[i].isFile() )
{
String tfname = allfiles[i].getName();
if ( ! tfname.endsWith(".htm"))
{
try
{
//System.out.println("id=" + FileID);
//{*[Replace the file in HTML BUF]*}
htmlcontent = htmlcontent.replaceAll(tfname,"readimage.jsp?infoid="+infoid+"&fileName=" + tfname);
//htmlcontent.replaceAll("html","readimage.jsp?id=" + new Long(FileID).toString());
}
catch(Exception e)
{
out.println("{*[Error occurred]*}: " + e.toString());
}
}
}
}
System.out.println("{*[Modify]*}HTML文件");
bytes = htmlcontent.getBytes();
OutputStream outstream= new java.io.FileOutputStream(HTMLFile);
outstream.write(bytes);
outstream.flush();
outstream.close();
System.out.println("{*[Save]*}HTML文件到HTML表");
}
String url=contextPath+"/info/info/view.do?id="+infoid;
response.sendRedirect(response.encodeRedirectUrl(url));
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -