81,090
社区成员
发帖
与我相关
我的任务
分享
mySmartUpload.initialize(config,request,response);
try {
// 上载
mySmartUpload.upload();
filename=mySmartUpload.getFiles().getFile(0).getFileName();
// 保存上载文件到指定目录
// PATH为form表单提交过来的
count = mySmartUpload.save(path);
filename=mySmartUpload.getRequest().getParameter("sourcefile");
//一定要放到mySmartUpload.save(path);后
num= mySmartUpload.getRequest().getParameter("num");
// 显示处理结果
out.println(count + "文件已上传至:"+path+"<br>");
} catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
out.println(num+"<br>"+filename+"<br>");
<HTML><BODY>
<FORM ENCTYPE="MULTIPART/FORM-DATA" METHOD="POST" ACTION="doservlet">
文件名称:
<INPUT TYPE="file" NAME="sourcefile"><BR>
分割文件个数:
<input type="text" name="num"><br>
<INPUT TYPE="submit" value="上传">
<INPUT TYPE="reset" value="清除">
<BR>
</FORM>
</BODY></HTML>
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
import FenBean.Fen;
public class doservlet extends HttpServlet {
private ServletConfig config;
//初始化Servlet
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
//处理GET请求
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("The method of the HTML form must be POST.");
}
//响应POST请求
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("GBK");
// 变量定义
int count=0;
String path=request.getRealPath("/");
String num="1";
String filename=null;
SmartUpload mySmartUpload = new SmartUpload();
// 初始化
mySmartUpload.initialize(config,request,response);
filename=mySmartUpload.getRequest().getParameter("sourcefile");
num= mySmartUpload.getRequest().getParameter("num");
out.println(filename+"<br>"+num+"<br>");
try {
// 上载
mySmartUpload.upload();
filename=mySmartUpload.getFiles().getFile(0).getFileName();
// 保存上载文件到指定目录
// PATH为form表单提交过来的
count = mySmartUpload.save(path);
// 显示处理结果
out.println(count + "文件已上传至:"+path+"<br>");
} catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
out.println(num+"<br>"+filename+"<br>");
//Fen myfileFen=new Fen();
//myfileFen.cut(path+filename,num);
}
}