📄 fileupload.java~1~
字号:
package com.j2ee.func.upload;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.text.SimpleDateFormat;
public class FileUpload {
private ServletRequest request;
private ServletResponse response;
private ServletConfig config;
private ServletContext app;
private ServletInputStream DATA;
private int FormSize;
private File f1;
private int filesize=0;
private FileOutputStream os;
private DataInputStream is;
private String path;
private String filename;
byte[] b;
byte t;
boolean flag = false;
public FileUpload(String path) {
this.path = path;
}
public void initialize(ServletContext application, ServletConfig config,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
this.request = request;
this.response = response;
this.config = config;
this.app = application;
DATA = request.getInputStream();
FormSize = request.getContentLength();
}
public void initialize(PageContext pageContext) throws IOException {
request = pageContext.getRequest();
app = pageContext.getServletContext();
response = pageContext.getResponse();
config = pageContext.getServletConfig();
DATA = request.getInputStream();
FormSize = request.getContentLength();
}
public boolean setPath(String lm){
if(lm.equals("ydm")){
path="../pcbmanager/upload/transfer/";
}
if(lm.equals("sj")){
path="../pcbmanager/upload/tjxx/";
}
if(lm.equals("yy")){
path="../pcbmanager/upload/product/";
}
return true;
}
public boolean setFilename(String s) {
try {
String path = app.getRealPath(s);
f1 = new File(path);
os = new FileOutputStream(f1);
} catch (FileNotFoundException e) {
return false;
}
return true;
}
public int getFilesize(){
return filesize;
}
public void getByte() {
int i = 0;
try {
is = new DataInputStream(DATA);
b = new byte[FormSize];
while (true) {
try {
t = is.readByte();
b[i] = t;
i++;
} catch (EOFException e) {
break;
}
}
is.close();
} catch (IOException e) {}
}
public boolean save() {
int i = 0, start1 = 0, start2 = 0;
String temp = "";
if (!flag) {
getByte();
flag = true;
}
try {
temp = new String(b, "ISO8859_1");
} catch (UnsupportedEncodingException e) {
return (false);
}
/*
start1=temp.indexOf("hf");
temp=temp.substring(start1);
start1=temp.indexOf("\r\n\r\n");
temp=temp.substring(start1+4);
start1=temp.indexOf("\r\n");
String path=temp.substring(0,start1);
*/
if(path==null){
start1=temp.indexOf("lm");
temp=temp.substring(start1);
start1=temp.indexOf("\r\n\r\n");
temp=temp.substring(start1+4);
start1=temp.indexOf("\r\n");
String lm=temp.substring(0,start1);
this.setPath(lm);
}
start1 = temp.indexOf("filename=\"");
temp = temp.substring(start1 + 10);
start1 = temp.indexOf("\"");
String fname = temp.substring(0, start1);
int bf = 0;
while ((bf = fname.indexOf("\\")) != -1) {
fname = fname.substring(bf + 1);
}
Date date = new Date();
SimpleDateFormat ff=new SimpleDateFormat("yyyymmddHHmmssSSS");
filename = ff.format(date) + fname;
path = path + filename;
setFilename(path);
start1 = temp.indexOf("\r\n\r\n");
temp = temp.substring(start1 + 4);
start2 = temp.indexOf(";\r\n");
if (start2 != -1) {
temp = temp.substring(0, start2);
}
try {
byte[] img = temp.getBytes("ISO8859_1");
for (i = 0; i < img.length; i++) {
os.write(img[i]);
}
os.close();
} catch (IOException e) {
return (false);
}
filesize=temp.length();
return (true);
}
public String getGetFilename() {
return filename;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -