package com.hope.common.util;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.upload.FormFile;
public class StringRandom {
public String getrandom() {
int num[] = new int[100];
int i;
for (i = 0; i < 100; i++) {
num[i] = (int) (1000 * Math.random());
}
return num[99] + "";
}
public String getStrDate() {
Date date = new Date();
String str = "" + date.getYear();
str = str + date.getMonth();
str = str + date.getDate();
str = str + date.getHours();
str = str + date.getMinutes();
str = str + date.getSeconds();
return str;
}
public String getPophoName() {
return this.getStrDate() + this.getrandom();
}
public String getFileName(String fileName) {
int a = fileName.indexOf(".");
while (a != -1) {
fileName = fileName.substring(a + 1);
a = fileName.indexOf(".");
}
fileName = this.getPophoName() + "." + fileName;
return fileName;
}
public String[] cunchu(HttpServletRequest request,
HttpServletResponse response, FormFile file) {
String encoding = request.getCharacterEncoding();
if (encoding != null && encoding.equalsIgnoreCase("utf-8"))
response.setContentType("text/html; charset=utf-8");
String fileName = "0";
String filePath = "";
String fjpath = "";
// String contentType = file.getContentType();
// String size = file.getFileSize() + " bytes";
String data = "";
String flag = "0";
try {
//建立一个上传文件的输出流
// 1M为:1049988 250M为:262497000
if (file.getFileSize() < 263497000 && file.getFileSize() > 0){
fileName = this.getFileName(file.getFileName());
filePath = request.getRealPath("/");
filePath = filePath + "move\\";
OutputStream bos1 = new FileOutputStream(filePath + fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
byte buffer[] = new byte[409600];
for (int bytesRead = 0; (bytesRead = stream.read(buffer, 0,
409600)) != -1;)
bos1.write(buffer, 0, bytesRead);
data = new String(baos.toByteArray());
flag = "1";
bos1.close();
stream.close();
baos.close();
file.destroy();
} else {
response.sendRedirect("tpaddok.jsp");
}
} catch (Exception fnfe) {
fnfe.printStackTrace();
}
fjpath = "move\\" + fileName;
String[] fileArray = { fileName + "", fjpath + "", flag };
return fileArray;
}
}