我想将一个简单的文件分割函数用servlet实现
编译没问题
执行却总是不成功
帮忙改正html页面为:通过浏览文件,输入文件分割单位大小,提交
<HTML><BODY>
<FORM METHOD="POST" ACTION="fenservlet">
文件名称:
<INPUT TYPE="FILE" NAME="sourcefile">
<BR>分割单位大小:
<INPUT TYPE="TEXT" NAME="size" >
<INPUT TYPE="SUBMIT" VALUE="分割">
</FORM>
</BODY></HTML>servlet代码
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class fenservlet extends HttpServlet{
private ServletConfig config;
//初始化Servlet
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
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.");
}
/*文件分割函数*/
public void cut(String fileName,String littlesize)throws Exception{
int size;
size = Integer.parseInt(littlesize)*1024;
int maxx = 0;
File inFile = new File(fileName);
int fileLength = (int)inFile.length(); //取得文件的大小
int value; //取得要分割的个数
RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件
value = fileLength/size;
int i=0;
int j=0;
//根据要分割的数目输出文件
for (;j<value;j++){
File outFile = new File(inFile.getName()+j+"part"); //分割文件后缀标记part
RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
maxx+=size;
for (;i<maxx;i++){
outt.write(inn.read());
}
outt.close();
}
File outFile = new File(inFile.getName()+j+"part"); //最后一部分
RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
for(;i<fileLength;i++){
outt.write(inn.read());
}
outt.close();
inn.close();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String fileName=request.getParameter("sourcefile");
String littlesize=request.getParameter("littlesize");
try{
cut(path+fileName,path+littlesize);
}catch(Exception e){out.println("分割失败");} }
}
编译没问题
执行却总是不成功
帮忙改正html页面为:通过浏览文件,输入文件分割单位大小,提交
<HTML><BODY>
<FORM METHOD="POST" ACTION="fenservlet">
文件名称:
<INPUT TYPE="FILE" NAME="sourcefile">
<BR>分割单位大小:
<INPUT TYPE="TEXT" NAME="size" >
<INPUT TYPE="SUBMIT" VALUE="分割">
</FORM>
</BODY></HTML>servlet代码
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class fenservlet extends HttpServlet{
private ServletConfig config;
//初始化Servlet
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
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.");
}
/*文件分割函数*/
public void cut(String fileName,String littlesize)throws Exception{
int size;
size = Integer.parseInt(littlesize)*1024;
int maxx = 0;
File inFile = new File(fileName);
int fileLength = (int)inFile.length(); //取得文件的大小
int value; //取得要分割的个数
RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件
value = fileLength/size;
int i=0;
int j=0;
//根据要分割的数目输出文件
for (;j<value;j++){
File outFile = new File(inFile.getName()+j+"part"); //分割文件后缀标记part
RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
maxx+=size;
for (;i<maxx;i++){
outt.write(inn.read());
}
outt.close();
}
File outFile = new File(inFile.getName()+j+"part"); //最后一部分
RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
for(;i<fileLength;i++){
outt.write(inn.read());
}
outt.close();
inn.close();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String fileName=request.getParameter("sourcefile");
String littlesize=request.getParameter("littlesize");
try{
cut(path+fileName,path+littlesize);
}catch(Exception e){out.println("分割失败");} }
}
点“分割”提交后,并没有网页后台的报错,而且没有分割文件
只是出现我调用cun()函数时抛出的异常“分割失败”
try{
cut(path+fileName,path+littlesize);
}catch(Exception e){out.println("分割失败");}
...
outt.close();
inn.close();
...顺序改一下试试:...
inn.close();
outt.close();
...最好再关闭的时候加上try{}catch(){}块。
想让别人帮你看你也要把信息贴出来啊,
try{
cut(path+fileName,path+littlesize);
}catch(Exception e){
e.printStackTrace();
out.println("分割失败");}
在html页面点击“分割”提交按钮后,链接页面就只是显示“分割失败”
并没有什么像404类似的网页错误
觉得应该是执行dopost中调用的cut()函数不成功后抛出的异常我那个cut()函数在dos下能执行,分割函数成功
现在问题是怎样将这个函数改成servlet实现
需要的可以到下载查“mvc实训-jsp+sevlet实现文件上传分割合并”