当前位置: 技术问答>java相关
用JAVA上传文件,文件大小问题,如何突破1MB!
来源: 互联网 发布时间:2015-08-01
本文导语: bean是从网上下载现成的,如下: package kingstar_www.chcn.bbs; import java.io.*; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; public class UpLoadFile { public UpLoadFile() { uploadDirectory = "."; ...
bean是从网上下载现成的,如下:
package kingstar_www.chcn.bbs;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class UpLoadFile
{
public UpLoadFile()
{
uploadDirectory = ".";
ContentType = "";
CharacterEncoding = "";
}
private String getFileName(String s)
{
int i = s.lastIndexOf("\");
if(i = s.length() - 1)
{
i = s.lastIndexOf("/");
if(i = s.length() - 1)
return s;
}
return s.substring(i + 1);
}
public void setUploadDirectory(String s)
{
uploadDirectory = s;
}
public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}
public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}
public void uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
uploadFile(httpservletrequest.getInputStream());
}
public void uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf(""")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}
private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
try
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
}
catch(IOException ioexception)
{
return null;
}
try
{
if(s == null)
return new String(abyte0, 0, ai[0]);
else
return new String(abyte0, 0, ai[0], s);
}
catch(Exception exception)
{
return null;
}
}
private static String newline = "n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;
}
谢谢各位了!
package kingstar_www.chcn.bbs;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class UpLoadFile
{
public UpLoadFile()
{
uploadDirectory = ".";
ContentType = "";
CharacterEncoding = "";
}
private String getFileName(String s)
{
int i = s.lastIndexOf("\");
if(i = s.length() - 1)
{
i = s.lastIndexOf("/");
if(i = s.length() - 1)
return s;
}
return s.substring(i + 1);
}
public void setUploadDirectory(String s)
{
uploadDirectory = s;
}
public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}
public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}
public void uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
uploadFile(httpservletrequest.getInputStream());
}
public void uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf(""")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}
private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
try
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
}
catch(IOException ioexception)
{
return null;
}
try
{
if(s == null)
return new String(abyte0, 0, ai[0]);
else
return new String(abyte0, 0, ai[0], s);
}
catch(Exception exception)
{
return null;
}
}
private static String newline = "n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;
}
谢谢各位了!
|
程序应该没有限制的,只是可能是服务器的缘故
我以前用PHP的时候有个2M的限制,要突破2M的限制,要更改php.ini中的设置。
在JSP中我还不是很清楚有没有这个限制。
我以前用PHP的时候有个2M的限制,要突破2M的限制,要更改php.ini中的设置。
在JSP中我还不是很清楚有没有这个限制。
|
如果是用JSP绝对没有这个限制,上传100M都没事