文件上传-ios端图片保存到后台(针对小格式图片)
- 格式:docx
- 大小:19.15 KB
- 文档页数:4
public class FileCommon{
/**
*
*@param file上传的文件
*@param fileName上传的文件名
*@param savePath需要保持的路径
*@return
*@throws FileNotFoundException
*/
@SuppressWarnings({ "deprecation", "unused" })
public static String upload(File file,String fileName, String savePath) throws FileNotFoundException
{
FileInputStream fis = null;
FileOutputStream fos = null;
String saveRootPath =
ServletActionContext.getRequest().getRealPath("/");
saveRootPath = saveRootPath+savePath;
File fileParent = new File(saveRootPath);// 创建保存文件的上级文件夹
if (!fileParent.exists())
{
boolean flag = fileParent.mkdirs();
if(!flag)
{
}
}
//String ex = StringTools.getExtention(fileName);
//String date = TimeTools.getStringByFormat(new Date(), "yyyyMMddHHmmss");
//fileName = name+"."+ex;
fis = new FileInputStream(file);
fos = new FileOutputStream(saveRootPath + fileName);
byte[] buffer = new byte[1024];
int len = 0;
try
{
while ((len = fis.read(buffer)) != -1)
{
fos.write(buffer, 0, len);
}
}
catch (FileNotFoundException e)
{
System.out.println("Exception: FileNotFoundException");
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Exception: IOExcption");
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
}
catch (IOException e)
{
System.out.println("FileInputStream close failed");
e.printStackTrace();
}
if (fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
System.out.println("FileOutputStream close failed");
e.printStackTrace();
}
}
}
return saveRootPath +fileName;
}
//读取ios端传输过来的附件信息
public static String sendData(String fileName) throws Exception { String data = null;
File file = new File(fileName);
if (file.exists()) {
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[is.available()];
is.read(bytes);
BASE64Encoder encoder = new BASE64Encoder();
data = encoder.encode(bytes);
System.out.println(data);
}
return data;
}
//解析ios端上传的文件,并保存到后台
@SuppressWarnings("deprecation")
public static String deal(String savePath,String fileName, String data) throws IOException {
String saveRootPath =
ServletActionContext.getRequest().getRealPath("/");
String middlePath = saveRootPath+savePath;
File file = new File(middlePath);
if (!file.exists())
{
boolean flag = file.mkdirs();
if(!flag)
{
}
}
OutputStream fos = new FileOutputStream(middlePath+fileName);
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(data);
InputStream is = new ByteArrayInputStream(bytes);
int length = 1024 * 50;
byte[] buff = new byte[length > is.available() ? is.available() : length];
while (is.read(buff) != -1) {
fos.write(buff);
if (length > is.available()) {